Android应用程序对手机的后退按钮做出正确反应,但在单击菜单的后退按钮时会出现nulpointerexception崩溃

时间:2014-12-02 17:15:22

标签: android android-intent android-activity listactivity application-restart

我有一个活动(比如说A2)从父活动中获取一个id(比如A1),并从数据库中获取结果后准备一个列表。点击一个列表项,它将另一个id传递给子活动(比方说A3)进行进一步处理。现在当我使用手机的原生后退按钮从A3回到A2时,一切都很棒,我看到A2上的数据,但如果我使用应用程序的菜单栏后退按钮应用程序崩溃给出nullpointerexception。我将以下代码作为参考......

活动A2

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_classes);

    class_details = new ArrayList<HashMap<String,String>>();
    TextView tv_institute_name = (TextView) findViewById(R.id.allclassess_schoolname);
    //List<NameValuePair> params = new ArrayList<NameValuePair>();
    Log.d("inside oncreate","inside oncreate");
    // prepairing the alert builder for application alert messages
    alertbuilder = new AlertDialog.Builder(AllClasses.this);

    // retreiving data from intent
    Bundle b = new Bundle();
    b = getIntent().getExtras();
    String imessage_string = b.getString("imessage");

    try  // preparing params for asynctask
    {
        imessage_json = new JSONObject(imessage_string);
        teacher_id=imessage_json.getString("teacherid");
        params.add(new BasicNameValuePair("teacherid",teacher_id));
        //Log.d("in allclasses, params",params.toString());

    }catch(JSONException e){
        //Log.d("json exception ","excepiton");
        e.printStackTrace();
    }


    new getAllClassess().execute(params); // asynctask called and set adapter to lv

    lv = new ListView(context);

    // Add list programmatically
    FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
     rootLayout.setFocusable(true);
     rootLayout.addView(lv);

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) { 
            Map map = new HashMap<String,String>();


            // getting values from selected ListItem
            String cid = ((TextView) view.findViewById(R.id.classid)).getText()
                    .toString();
           // String teacher_id=params.get(0).getValue("teacherid");
            map.put("classid", cid);
            map.put("teacherid", teacher_id);

            JSONObject j = new JSONObject(map);
            Bundle b = new Bundle();
            b.putString("imessage", j.toString()); 

          //  String[] imessage_array = {cid,teacher_id};


            Intent i= new Intent(context,ClassRecordingDetails.class);

           // i.putExtra("imessage", imessage_array);
            i.putExtras(b);
            startActivity(i);
        }

protected void onRestart(){
    super.onRestart();

    Log.d("inside restart","restarted");
}

@Override
protected void onResume(){
    super.onResume();
    Log.d("inside onresume","resumed");
}

活动A3:截至目前我在A3上的表现并不算什么

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_class_recording_details);

    //Bundle b = new Bundle();
    b = getIntent().getExtras();
    String imessage_string= b.getString("imessage");
    Log.d("in recording details",imessage_string);
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (menuItem.getItemId()) {
    case android.R.id.home:
      Intent homeIntent = new Intent(context, AllClasses.class);
      homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      homeIntent.putExtras(b);
      //startActivity(homeIntent); 
      NavUtils.navigateUpTo(getParent(), homeIntent);
    }
    return super.onOptionsItemSelected(menuItem);

Cat Log

12-03 02:49:19.144: D/AndroidRuntime(3625): Shutting down VM
12-03 02:49:19.144: W/dalvikvm(3625): threadid=1: thread exiting with uncaught exception (group=0x41f69d40)
12-03 02:49:19.157: E/AndroidRuntime(3625): FATAL EXCEPTION: main
12-03 02:49:19.157: E/AndroidRuntime(3625): Process: com.example.rrd.school.project, PID: 3625
12-03 02:49:19.157: E/AndroidRuntime(3625): java.lang.NullPointerException
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.support.v4.app.NavUtilsJB.navigateUpTo(NavUtilsJB.java:33)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.support.v4.app.NavUtils$NavUtilsImplJB.navigateUpTo(NavUtils.java:120)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.support.v4.app.NavUtils.navigateUpTo(NavUtils.java:202)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at com.example.rrd.school.project.ClassRecordingDetails.onOptionsItemSelected(ClassRecordingDetails.java:47)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.app.Activity.onMenuItemSelected(Activity.java:2608)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.view.View.performClick(View.java:4456)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.view.View$PerformClick.run(View.java:18465)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.os.Handler.handleCallback(Handler.java:733)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.os.Looper.loop(Looper.java:136)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at android.app.ActivityThread.main(ActivityThread.java:5086)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at java.lang.reflect.Method.invoke(Method.java:515)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-03 02:49:19.157: E/AndroidRuntime(3625):     at dalvik.system.NativeStart.main(Native Method)
12-03 02:49:28.898: I/Process(3625): Sending signal. PID: 3625 SIG: 9
12-03 02:49:30.236: I/httpresponse(4242): org.apache.http.conn.EofSensorInputStream@428f80c8
12-03 02:49:30.238: I/json_string_block=(4242): {"classdetails":[{"classid":"1","classname":"12th C"},{"classid":"2","classname":"12th B"},{"classid":"3","classname":"12th A"}],"success":1,"instituteid":"inst1","institutename":"instname1","teacherid":"teacher1","teachername":"teachername"}
12-03 02:49:30.243: I/class_details=(4242): [{classname=12th C, classid=1}, {classname=12th B, classid=2}, {classname=12th A, classid=3}]

非常感谢任何解决问题的帮助......谢谢!

2 个答案:

答案 0 :(得分:1)

好吧......好吧,结果一试。我必须在清单文件中为下面的活动A2(所有类)设置launchMode = singleTop属性,以便系统在后台堆栈中保持为A2活动创建的实例

 android:launchMode="singleTop"

然后从活动A3调用navUtils.navigateUpFromSameTask(this)方法从backstack调用相同的实例,而不是创建新实例。

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(menuItem);
}
嗯...简单的解决方案haa ...:D ...谢谢你的帮助...... !!

答案 1 :(得分:0)

尝试 getParentActivityIntent()代替getParent()