Android:按下后退按钮时出错,即使在完成上一个活动后也是如此

时间:2013-04-25 09:10:20

标签: android android-intent

我在使用getIntent.getextra()的行附近得到nullpointer异常。

这是错误日志:

 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“))”。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

你开始活动2次。第一个已启动的活动会引发NullPointerException,因为未为该活动的title11设置Intent

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