gpt4 book ai didi

android - ListView 适配器使 Activity 崩溃

转载 作者:行者123 更新时间:2023-12-02 06:16:47 26 4
gpt4 key购买 nike

如主题中所示。当我评论 listView.setAdapter(adapter); 它只是显示另一个 Activity :

这是整个 onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista__klas);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}

klasy = getResources().getStringArray(R.array.klasy);

ListView listView = (ListView)findViewById(R.id.lista_klas);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_lista__klas);
listView.setAdapter(adapter);
}

当我按下启动此 Activity 的按钮时,从 logcat 中记录:

11-20 13:14:53.085    1494-1780/system_process I/ActivityManager﹕ START {cmp=pl.krasiniak.krachapp_beta/.Lista_Klas} from pid 3975
11-20 13:14:53.335 3975-3975/pl.krasiniak.krachapp_beta D/AndroidRuntime﹕ Shutting down VM
11-20 13:14:53.335 3975-3975/pl.krasiniak.krachapp_beta W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40a521f8)
11-20 13:14:53.345 3975-3975/pl.krasiniak.krachapp_beta E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{pl.krasiniak.krachapp_beta/pl.krasiniak.krachapp_beta.Lista_Klas}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
at android.app.ActivityThread.access$600(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at pl.krasiniak.krachapp_beta.Lista_Klas.onCreate(Lista_Klas.java:34)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
            at android.app.ActivityThread.access$600(ActivityThread.java:132)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4575)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
11-20 13:14:53.355 1494-1675/system_process W/ActivityManager﹕ Force finishing activity pl.krasiniak.krachapp_beta/.Lista_Klas
11-20 13:14:53.355 1494-1675/system_process W/ActivityManager﹕ Force finishing activity pl.krasiniak.krachapp_beta/.MainActivity
11-20 13:14:53.865 1494-1509/system_process W/ActivityManager﹕ Activity pause timeout for ActivityRecord{40ef3f48 pl.krasiniak.krachapp_beta/.Lista_Klas}
11-20 13:14:54.065 3215-3215/com.cyanogenmod.trebuchet W/Adreno200-EGLSUB﹕ SetSwapInterval() interval: 0 not set
11-20 13:14:54.105 1494-1506/system_process I/ActivityManager﹕ No longer want com.android.keychain (pid 3950): hidden #16
11-20 13:15:04.105 1494-1509/system_process W/ActivityManager﹕ Activity destroy timeout for ActivityRecord{40ebf3c0 pl.krasiniak.krachapp_beta/.MainActivity}
11-20 13:15:04.105 1494-1509/system_process W/ActivityManager﹕ Activity destroy timeout for ActivityRecord{40ef3f48 pl.krasiniak.krachapp_beta/.Lista_Klas}

我确定数组不为空。谢谢您的回答,问候:)

最佳答案

代码不正确:

setContentView(R.layout.activity_lista__klas);

// ...

ListView listView = (ListView)findViewById(R.id.lista_klas);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_lista__klas);

内容 View 和数组适配器使用相同的布局。当 two-argument constructor of ArrayAdapter使用时,资源 ID 必须解析为单个 TextViewfindViewById() 调用意味着它是一个带有 ListView 的更复杂的布局。

如果我正确理解您的 Intent ,请执行以下操作:

  • 确保 activity_list__klas 包含具有指定 id lista_klasListView,以便 findViewById() > 返回非null

  • 将适配器构造函数的布局资源 ID 更改为包含表示列表行的单个 TextView 的资源文件。或者更改为另一个构造函数重载,该重载允许您指定映射数组项的 TextView

  • 向适配器提供一些数据以实际显示某些内容。

关于android - ListView 适配器使 Activity 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20097129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com