我在我的Android代码中遇到以下异常,但我无法弄明白。我已使用TabActivity
扩展了我的主页活动,并从MainActivity
接收了其中的意图。在tab3中,我已经将意图传递给另一个活动,该活动将在选择tab3时开始。来自该意图的数据正在该活动中收到。我已经通过Log检查了它。收到的数据将显示在该活动的ListView
中。但不幸的是我的应用程序崩溃了。对此有何帮助?
这是我的logcat。
01-24 14:36:34.880: E/AndroidRuntime(1147): FATAL EXCEPTION: main
01-24 14:36:34.880: E/AndroidRuntime(1147): Process: com.example.app, PID: 1147
01-24 14:36:34.880: E/AndroidRuntime(1147): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Home1}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.CameraList}: java.lang.NullPointerException
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.os.Handler.dispatchMessage(Handler.java:102)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.os.Looper.loop(Looper.java:137)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-24 14:36:34.880: E/AndroidRuntime(1147): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 14:36:34.880: E/AndroidRuntime(1147): at java.lang.reflect.Method.invoke(Method.java:515)
01-24 14:36:34.880: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-24 14:36:34.880: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-24 14:36:34.880: E/AndroidRuntime(1147): at dalvik.system.NativeStart.main(Native Method)
01-24 14:36:34.880: E/AndroidRuntime(1147): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.CameraList}: java.lang.NullPointerException
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2019)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.widget.TabHost.setCurrentTab(TabHost.java:413)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.widget.TabHost.addTab(TabHost.java:240)
01-24 14:36:34.880: E/AndroidRuntime(1147): at com.example.app.Home1.setNewTab(Home1.java:136)
01-24 14:36:34.880: E/AndroidRuntime(1147): at com.example.app.Home1.onCreate(Home1.java:83)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.Activity.performCreate(Activity.java:5243)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-24 14:36:34.880: E/AndroidRuntime(1147): ... 11 more
01-24 14:36:34.880: E/AndroidRuntime(1147): Caused by: java.lang.NullPointerException
01-24 14:36:34.880: E/AndroidRuntime(1147): at com.example.app.CameraList.onCreate(CameraList.java:40)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.Activity.performCreate(Activity.java:5243)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-24 14:36:34.880: E/AndroidRuntime(1147): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-24 14:36:34.880: E/AndroidRuntime(1147): ... 22 more
这是我在家庭活动中使用标签的地方..
String [] names;
names = intent.getStringArrayExtra("name");
if(tag.equals("tab3"))
{
Intent ListIntent = new Intent(this, List.class);
ListIntent.putExtra("itemName", names);
tabSpec.setContent(ListIntent);
tabHost.addTab(tabSpec);
}
我在OnCreate方法中接收意图的活动:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cam_list);
//Log.i("cam","List");
Intent intent = getIntent();
name = intent.getStringArrayExtra("camName");
//Log.i("cam", name[1]);
url = intent.getStringArrayExtra("url");
list = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.cam_list,R.id.text1, name);
list.setAdapter(adapter);
/*list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
String itemValue = (String) list.getItemAtPosition(position);
String camUrl = url[position];
// Show Alert
Toast.makeText(getApplicationContext(),camUrl,Toast.LENGTH_LONG).show();
}
});*/
}
答案 0 :(得分:1)
如果您从NullPointerException
检索到的View
获得findViewById()
,则Android会告诉您View
不存在。这里有两种主要的可能性:
View
确实不存在,例如在您需要时无法拨打setContentView()
。
如果您使用的是Eclipse,有时R
常量会与APK文件的内容不同步。要解决此问题,请选择项目&gt;从Eclipse主菜单中清除,然后再次尝试运行您的应用程序。幸运的是,我没有在Android Studio中看到这种行为(感谢$DEITY
获得小优惠......)。