adding Textviews and radio button in code

by automerc » Thu, 05 Mar 2009 10:15:09 GMT


Sponsored Links
 I was wondering how we could add objects such as textview and radio
buttons to the activity through code and not the XML file and still
have it run correctly. I tried adding a simple textview below but I
got an error when I tried to run. Can any1 tell me how I could add the
textview in correctly?

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TextView;

public class Q_end extends Activity {
    /** Called when the activity is first created. */
        private ArrayList<String> qs = new ArrayList<String>(10);
        private ArrayList<String[]> as = new ArrayList<String[]>(10);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.end_app);
        ScrollView we = (ScrollView) findViewById(R.id.viewer);
        String[] temp = new String[4];
        temp[0] = "the";
        temp[1] = "the1";
        temp[2] = "the2";
        temp[3] = "the3";
        as.add(temp);
        String temp2 = "the rocks says";
        qs.add(temp2);
        qs.add("the question is");
        String[] temp3 = new String[1];
        temp3[0] = " ";
        as.add(temp3);
        TextView gg = new TextView(null);
        gg.setText(qs.get(0));
        we.addView(gg);

    }
}
--~--~---------~--~----~------------~-------~--~----~



adding Textviews and radio button in code

by Mark Murphy » Thu, 05 Mar 2009 10:38:05 GMT


 


Could you provide us with the error details from the log? Use adb
logcat, Eclipse, or DDMS.

In particular, it would be useful to know what exception occurred and
what line of your source it occurred on.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Android Training in Sweden --  http://www.sotrium.com/training.php 

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


adding Textviews and radio button in code

by automerc » Fri, 06 Mar 2009 08:54:26 GMT


  think this is what caused the error

03-05 19:50:55.975: DEBUG/dalvikvm(224): VM cleaning up
03-05 19:50:55.975: DEBUG/dalvikvm(224): LinearAlloc 0x0 used 541260
of 4194304 (12%)
03-05 19:50:56.014: ERROR/dalvikvm(224): pthread_setspecific failed,
err=22
03-05 19:50:56.045: INFO/jdwp(232): received file descriptor 10 from
ADB
03-05 19:50:56.334: DEBUG/AndroidRuntime(232): Shutting down VM
03-05 19:50:56.334: WARN/dalvikvm(232): threadid=3: thread exiting
with uncaught exception (group=0x40010e28)
03-05 19:50:56.346: ERROR/AndroidRuntime(232): Uncaught handler:
thread main exiting due to uncaught exception
03-05 19:50:56.364: ERROR/AndroidRuntime(232):
java.lang.RuntimeException: Unable to start activity ComponentInfo
{application.main/application.main.Q_end}:
java.lang.NullPointerException
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2140)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2156)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread.access$1800(ActivityThread.java:112)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.os.Handler.dispatchMessage(Handler.java:88)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.os.Looper.loop(Looper.java:123)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread.main(ActivityThread.java:3742)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
java.lang.reflect.Method.invokeNative(Native Method)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
java.lang.reflect.Method.invoke(Method.java:515)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:739)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
dalvik.system.NativeStart.main(Native Method)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): Caused by:
java.lang.NullPointerException
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.view.View.<init>(View.java:1645)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.widget.TextView.<init>(TextView.java:223)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.widget.TextView.<init>(TextView.java:217)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.widget.TextView.<init>(TextView.java:212)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
application.main.Q_end.onCreate(Q_end.java:32)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1122)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2103)
03-05 19:50:56.364: ERROR/AndroidRuntime(232): ... 11 more
03-05 19:50:56.374: INFO/Process(51): Sending signal. PID: 232 SIG: 3
03-05 19:50:56.405: INFO/dalvikvm(232): threadid=7: reacting to signal
3
03-05 19:50:56.426: INFO/dalvikvm(232): Wrote stack trace to '/data/
anr/traces.txt'
03-05 19:51:05.897: WARN/ActivityManager(51): Launch timeout has
expired, giving up



adding Textviews and radio button in code

by automerc » Sat, 07 Mar 2009 05:22:25 GMT


 re there any examples of adding textviews and radio button through
code?

On Mar 5, 7:54pm, automerc <bigautosur...@gmail.com> wrote:
--~--~---------~--~----~------------~-------~--~----~