我正在设置一个简单的列表视图,还没有填充真实数据。但是,当尝试运行测试时,我每次都会得到一个空指针异常。我无法弄清楚是什么导致了问题。错误日志抱怨 setLayoutParams。这是我的代码:
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
}
});
}
日志猫:
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)的列表视图,感谢所有帮助!