我正在建立一個簡單的列表視圖,但沒有真正的數據被填充到它。但是,當試圖運行測試時,我每次都得到一個空指針異常。我無法弄清楚導致問題的原因。錯誤日誌抱怨setLayoutParams。這裏是我的代碼:Android ListView返回空
public class Groups extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.groups_listviews);
ListView lView = (ListView)findViewById(R.id.list_view_1);
lView.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, R.layout.groups_listviews_row));
lView.setCacheColorHint(0);
lView.setAdapter(null);
lView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
的logcat:
06-20 11:03:27.044: D/dalvikvm(3641): GC_EXTERNAL_ALLOC freed 64K, 43% free 3080K/5379K, external 2357K/2773K, paused 81ms
06-20 11:03:28.388: D/dalvikvm(3641): GC_EXTERNAL_ALLOC freed 6K, 43% free 3096K/5379K, external 4457K/4816K, paused 28ms
06-20 11:03:28.442: D/AndroidRuntime(3641): Shutting down VM
06-20 11:03:28.442: W/dalvikvm(3641): threadid=1: thread exiting with uncaught exception (group=0x40018560)
06-20 11:03:28.442: E/AndroidRuntime(3641): FATAL EXCEPTION: main
06-20 11:03:28.442: E/AndroidRuntime(3641): java.lang.RuntimeException: Unable to start activity ComponentInfo{com/com.main.Groups}: java.lang.NullPointerException
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.os.Looper.loop(Looper.java:130)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread.main(ActivityThread.java:3806)
06-20 11:03:28.442: E/AndroidRuntime(3641): at java.lang.reflect.Method.invokeNative(Native Method)
06-20 11:03:28.442: E/AndroidRuntime(3641): at java.lang.reflect.Method.invoke(Method.java:507)
06-20 11:03:28.442: E/AndroidRuntime(3641): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-20 11:03:28.442: E/AndroidRuntime(3641): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-20 11:03:28.442: E/AndroidRuntime(3641): at dalvik.system.NativeStart.main(Native Method)
06-20 11:03:28.442: E/AndroidRuntime(3641): Caused by: java.lang.NullPointerException
06-20 11:03:28.442: E/AndroidRuntime(3641): at com.main.Groups.onCreate(Groups.java:23)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-20 11:03:28.442: E/AndroidRuntime(3641): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
06-20 11:03:28.442: E/AndroidRuntime(3641): ... 11 more
答:我在訪問不正確的佈局文件(list_view_1)的列表視圖,感謝所有幫助!
看起來這是返回null,我會看看list_view_1。 – Jamesar
明白了,就是這樣,顯然我正在訪問不同佈局文件的listview(list_view_1)。謝謝你的幫助! – Jamesar
沒有問題,很高興我可以幫忙;) –