我嘗試使用可能不同的方式打開我的listView活動,這些方法可以在Google搜索時從Android Dev Resources找到。每次它不起作用,並給我錯誤。Android:打開列表查看活動
我試圖打開一個新的活動與列表,當我點擊一個按鈕從主要活動。
我今天剛剛開始用Android編程,所以詳細的解釋會對我有很大的幫助。
我想我已經把問題指向了JokesActivity.java(下) 如果我需要從我的程序發佈更多的文件,請評論,我會發布它們。
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>
你能多談談你所期望的要發生的事情,發生了什麼?您在問題中提及「帶按鈕」,但在細節中看不到任何按鈕。 – Iain
更多細節將會很棒。像samir說你應該調用setContentView。提供你獲得的真正幫助的錯誤。 – Stevy888