我尝试使用谷歌搜索和 Android 开发资源中发现的不同方式打开我的 listView 活动。每次它都不起作用并给我错误。
当我单击主要活动中的按钮时,我正在尝试使用列表打开一个新活动。
我今天刚开始用 Android 编程,所以详细的解释会对我有很大帮助。
我想我已经将问题定位在 JokesActivity.java(下)中的某个地方如果我需要从我的程序中发布更多文件,请发表评论,我会发布它们。
笑话活动:
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"
};
}
日志猫:
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>
主要的.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>