Android:打开listView活动

时间:2012-05-26 16:11:52

标签: java android android-listview

我尝试使用Googling和Android Dev Resources中找到的不同方式打开我的listView活动。每次它不起作用并给我错误。

当我点击主要活动中的按钮时,我试图用列表打开一个新活动。

我今天刚刚开始使用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>

1 个答案:

答案 0 :(得分:1)

您应该添加setContentView(R.layout.main) first

 setContentView(R.layout.main);
 ListView listView = (ListView) findViewById(R.id.mylist);

<强>更新

将listview放在main.xml

 <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>