1

我在使用 getIntent.getextra() 的行附近的某处遇到空指针异常。

这是错误日志:

 04-25 14:19:43.718: E/AndroidRuntime(10031): FATAL EXCEPTION: main
 04-25 14:19:43.718: E/AndroidRuntime(10031): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.attendence/com.example.attendence.UserPage}:  java.lang.NullPointerException
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.os.Handler.dispatchMessage(Handler.java:99)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.os.Looper.loop(Looper.java:130)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread.main(ActivityThread.java:3687)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at java.lang.reflect.Method.invokeNative(Native Method)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at java.lang.reflect.Method.invoke(Method.java:507)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at dalvik.system.NativeStart.main(Native Method)
 04-25 14:19:43.718: E/AndroidRuntime(10031): Caused by: java.lang.NullPointerException
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at com.example.attendence.UserPage.onCreate(UserPage.java:52)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
 04-25 14:19:43.718: E/AndroidRuntime(10031):   ... 11 more

这是我用来在我的第一个活动中调用第二个活动(称为 UserPage)的代码:

            startActivity(new Intent(HelloUser.this, UserPage.class));
            intent= new Intent(HelloUser.this, UserPage.class);
            intent.putExtra("title11", "p");
            intent.putExtra("name", et.getText().toString().trim());
            startActivity(intent); 
            finish();

如您所见,我正在调用finish(),因此后退按钮不应在下一个活动中起作用。

这是称为 Userpage 的第二个活动,其中出现错误:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.userpage);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    title11= (String)getIntent().getStringExtra("title11");
    name= (String)getIntent().getStringExtra("name");
    if(title11.equals("new"))
    {
        title11=null;
        UserPage.this.runOnUiThread(new Runnable() {
            public void run() {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserPage.this);
                builder.setTitle("Welcome");
                builder.setMessage("Hello "+name)  
                       .setCancelable(false)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                           }
                       });                     
                AlertDialog alert = builder.create();
                alert.show();               
            }
        });
    }
    t1=(TextView)findViewById(R.id.titlename);
    t2=(TextView)findViewById(R.id.gname);
    t1.setText(name);
    t2.getBackground().setAlpha(55);

    t2.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {

           startActivity(new Intent(UserPage.this, CreateGroup.class));
       }
    });

}
}

当我按下返回按钮时,“if(title11.equals("new"))”行出现错误。

4

2 回答 2

4
startActivity(new Intent(HelloUser.this, UserPage.class));
            intent= new Intent(HelloUser.this, UserPage.class);
            intent.putExtra("title11", "p");
            intent.putExtra("name", et.getText().toString().trim());
            startActivity(intent); 
            finish();

您启动了两次活动,所以第一个开始的找不到title11。试试这个:

 intent= new Intent(HelloUser.this, UserPage.class);
                intent.putExtra("title11", "p");
                intent.putExtra("name", et.getText().toString().trim());
                startActivity(intent); 
                finish();
于 2013-04-25T09:16:15.640 回答
0

你开始了 2 次活动。NullPointerException由第一个启动的活动引发,因为没有title11为该活动设置Intent.

此外,如果完成的唯一目的是确保 HelloUser 活动永远不会进入“后退按钮历史记录”,那么最好android:noHistory="true"在清单中为该活动设置而不是当前的实现。

于 2013-04-25T09:20:56.793 回答