java - Android:打开listView Activity

标签 java android android-listview

我尝试使用谷歌搜索和 Android 开发资源中找到的不同方式打开我的 listView Activity 。每次它都不起作用并给我错误。

当我单击主 Activity 中的按钮时,我尝试打开一个带有列表的新 Activity 。

我今天刚刚开始使用 Android 编程,所以详细的解释会对我有很大帮助。

我想我已经将问题定位到 JokesActivity.java 中的某个位置(如下) 如果我需要从我的程序中发布更多文件,请发表评论,我将发布它们。

笑话 Activity :

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>

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>

最佳答案

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

 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>

关于java - Android:打开listView Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10767848/

相关文章:

java - 安卓工作室 : help RecyclerView Click does not work

android - Listview与内容高度相同

java - 类似 Django 的 Java 框架

java - 将 C 源 CRC16CITT 函数转换为 Java

java - 在 Java 中需要带有文件类型过滤器的 FileDialog

java - java中电影名称的字符串过滤

android - 如何在ListView中显示带有列的对象数组android?

android - 资源中图像的缩略图

android - ListView项目动画: adding one by one with some delay

java - 当主要 Activity 继承 ListActivity 类时,应用程序不起作用