空指针异常创建意图

时间:2012-02-29 19:56:13

标签: java android android-intent

我正在尝试为应用程序上的新活动创建一个intent,但是在创建活动时会抛出空指针异常,这会强制模拟器关闭。有任何想法吗?

    ...
    ...
//connect objects with the xml view
searchbar = (EditText) findViewById(R.id.searchtxt);
btmsearch = (Button) findViewById(R.id.searchbtm);
searchopt = (TextView) findViewById(R.id.advsearchtxt);

searchopt.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent bi = new Intent(getApplicationContext(), adv_search.class);
        startActivity(bi);
    }
});
//make the search when click on search buttom
btmsearch.setOnClickListener(new View.OnClickListener() {

    ...
    ...

应用程序的logcat如下:

02-29 19:54:42.313: D/AndroidRuntime(628): Shutting down VM
02-29 19:54:42.313: W/dalvikvm(628): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
02-29 19:54:42.313: E/AndroidRuntime(628): Uncaught handler: thread main exiting due to uncaught exception
02-29 19:54:42.323: E/AndroidRuntime(628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.remotedata.firstapp/com.remotedata.firstapp.DataconectActivity}: java.lang.NullPointerException
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread.access$2100(ActivityThread.java:116)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.os.Looper.loop(Looper.java:123)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread.main(ActivityThread.java:4203)
02-29 19:54:42.323: E/AndroidRuntime(628):  at java.lang.reflect.Method.invokeNative(Native Method)
02-29 19:54:42.323: E/AndroidRuntime(628):  at java.lang.reflect.Method.invoke(Method.java:521)
02-29 19:54:42.323: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-29 19:54:42.323: E/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
02-29 19:54:42.323: E/AndroidRuntime(628):  at dalvik.system.NativeStart.main(Native Method)
02-29 19:54:42.323: E/AndroidRuntime(628): Caused by: java.lang.NullPointerException
02-29 19:54:42.323: E/AndroidRuntime(628):  at com.remotedata.firstapp.DataconectActivity.onCreate(DataconectActivity.java:54)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-29 19:54:42.323: E/AndroidRuntime(628):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
02-29 19:54:42.323: E/AndroidRuntime(628):  ... 11 more
02-29 19:54:42.334: I/dalvikvm(628): threadid=7: reacting to signal 3
02-29 19:54:42.334: E/dalvikvm(628): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

2 个答案:

答案 0 :(得分:1)

根据您提供的代码,只有两行可以产生NullPointerException

要么因为searchopt为空(R.id.searchtxt是否属于你当前的布局?) 或者btmsearch为空(R.id.searchbtm是否属于您当前的布局?)

答案 1 :(得分:0)

以下所有内容实际上是一行代码:

searchopt.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent bi = new Intent(getApplicationContext(), adv_search.class);
        startActivity(bi);
    }
});

由于在到达该行代码时评估大括号内部没有任何内容,如果错误在该行上,则该行必须为searchopt为空。您需要重新检查布局:确保在该布局中确实存在ID为advsearchtxt的视图,并确保它实际上是TextView的实例。