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:
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. AppWidget updates

I am trying to update an AppWidget manually when a user setting
changes on the device. To do so I use code similar to this:

ComponentName thisWidget = new ComponentName(this, MyAppWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);

I can tell that my AppWidget onUpdate method is being called as a
result since I log some debug strings to logcat in the code. However
the AppWidget itself doesn't change on the screen.

Here comes the interesting part: if I rotate my device and force a
refresh of the home screen (from PORTRAIT to LANDSCAPE or vice-versa)
then finally my AppWidget gets updated. However rotating the device
does not trigger the onUpdate method to be called, so the AppWidget
must be using the RemoteViews provided in the earlier update.

Can somebody explain me what to do to force the home screen redraw of
my AppWidget when it processes an update????

Thanks.

-- 

2. Trying to diagnose a Resource Not Found exception

I'm getting the following exception message:
"android.content.res.Resources$NotFoundException: Resource ID
#0x7f070004 type #0x12 is not valid"

First, I'm not sure if this error is due to a bad resource ID or a bad
resource type. The resource that is associated with this error is:
R.id.chooseTableRowTextView, which I verified is equal to #0x7f070004.
The id comes from the following xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/chooseTabelRowLinearLayout"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="None set" android:id="@+id/
chooseTableRowTextView" android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>

I'm building a ListView which gets data from an ArrayAdapter. Since
the ArrayAdapter data comes from a remote database query that can take
a long time, I'm building the ArrayAdapter in an AsyncTask then
calling setListAdapter from within AsyncTask.onPostExecute. The
ArrayAdapter is built like this:

tableListAdapter = new
ArrayAdapter<String>(this,R.id.chooseTableRowTextView,tableNames);

Where tableNames is the actual array of strings returned from the
remote database.

The exception comes sometime after I've done all the setup when the
system is trying to draw the ListView. I'm at a loss how to debug from
here. Any suggestions?

-- 

3. Disable default button click sound

4. Reading precomputed vertex arrays in application?

5. stringindexoutofboundsexception in ViewRoot.draw(boolean)

6. Themes and styles

7. Controlling Navigation Volume