Reputation: 388
I have tried opening up my listView activity using may different ways that I have found while Googling and from the Android Dev Resources. Each time it doesn't work and gives me errors.
I am trying to open a new activity with a list when I click on a button from the main activity.
I have just started programming with Android today so detailed explanations would help me a lot.
I think I have pinpointed the problem to somewhere in JokesActivity.java(below) If I need to post more files from my program please comment and I will post them.
JokesActivity:
public class JokesActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = (ListView) findViewById(R.id.mylist);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, CATEGORIES);
listView.setAdapter(adapter);
}
String[] CATEGORIES = new String[] {
"Blonde", "Chuck Norris"
};
}
Logcat:
05-26 11:19:51.236: D/AndroidRuntime(12173): Shutting down VM
05-26 11:19:51.236: W/dalvikvm(12173): threadid=1: thread exiting with uncaught exception (group=0x40018560)
05-26 11:19:51.236: E/AndroidRuntime(12173): FATAL EXCEPTION: main
05-26 11:19:51.236: E/AndroidRuntime(12173): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lulzone/com.lulzone.JokesActivity}: java.lang.NullPointerException
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.os.Looper.loop(Looper.java:130)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread.main(ActivityThread.java:3806)
05-26 11:19:51.236: E/AndroidRuntime(12173): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:19:51.236: E/AndroidRuntime(12173): at java.lang.reflect.Method.invoke(Method.java:507)
05-26 11:19:51.236: E/AndroidRuntime(12173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-26 11:19:51.236: E/AndroidRuntime(12173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-26 11:19:51.236: E/AndroidRuntime(12173): at dalvik.system.NativeStart.main(Native Method)
05-26 11:19:51.236: E/AndroidRuntime(12173): Caused by: java.lang.NullPointerException
05-26 11:19:51.236: E/AndroidRuntime(12173): at com.lulzone.JokesActivity.onCreate(JokesActivity.java:19)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 11:19:51.236: E/AndroidRuntime(12173): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
05-26 11:19:51.236: E/AndroidRuntime(12173): ... 11 more
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_weight="1.00"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:text="@string/button_jokes"
android:onClick="openJokes"/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:text="@string/button_facts" />
</LinearLayout>
<LinearLayout
android:layout_weight="1.00"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:text="@string/button_quotes" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.00"
android:text="@string/button_more" />
</LinearLayout>
Upvotes: 0
Views: 136
Reputation: 40416
you should add setContentView(R.layout.main) first
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
UPDATE
put listview in main.xml
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
Upvotes: 1