与子飞机一样。当我评论listView.setAdapter(adapter);
时,它只显示另一项活动:
这完全是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);
}
当我按下开始此活动的按钮时,从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}
我确定数组不是空的。谢谢你的回答,问候:)
答案 0 :(得分:0)
试试这个。添加第三个参数
adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.activity_lista__klas,klasy);
如果这不起作用。请粘贴您的布局文件。
答案 1 :(得分:0)
我猜你的问题就在这里:
ListView listView = (ListView)findViewById(R.id.lista_klas);
检查您是否拥有包含此ID的ListView
且listView
不为空。
此外,在创建适配器时,您需要传递ArrayAdapter
包含单个TextView
的布局文件,以用于显示每个列表项。您已经传递了在setContentView
中使用的相同布局,其中(我假设)是您活动的完整布局。
答案 2 :(得分:0)
代码不正确:
setContentView(R.layout.activity_lista__klas);
// ...
ListView listView = (ListView)findViewById(R.id.lista_klas);
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_lista__klas);
内容视图和数组适配器使用相同的布局。使用two-argument constructor of ArrayAdapter
时,资源ID必须解析为单个TextView
。 findViewById()
调用意味着它是一个更复杂的布局,其中包含ListView
。
如果我理解您的意图正确,请执行以下操作:
确保activity_list__klas
包含具有指定ID ListView
的{{1}},以便lista_klas
返回非findViewById()
。
将适配器构造函数的布局资源ID更改为包含表示列表行的单个null
的资源文件。或者更改为另一个构造函数重载,允许您指定映射数组项的TextView
。
向适配器提供一些数据以实际显示内容。