android app reacts properly to to phone's back button but crashes with nulpointerexception when clicked on menu's back button

非 Y 不嫁゛ 提交于 2019-12-13 04:43:04

问题


i have an activity (say A2) which gets an id from its parent activity (say A1) and after fetching results from database prepares a list. on clicking a list item it passes the another id to a child activity (say A3) for further processing.Now when i come back from A3 to A2 using phone's native back button everything is awesome and i see the data on A2 but if i use the app's menu bar back button the app crashes giving nullpointerexception. i am putting the code below for reference...

Activity 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");
}

Activity A3 : as of now nothing i am performing on 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}]

any help to resolve the issue is greatly appreciated...thanks !


回答1:


okay...so it worked out. i had to set the launchMode=singleTop property inside the manifest file for activity A2(allclasses) like below so that system keeps instance created for A2 activity alive in backstack

 android:launchMode="singleTop"

and then just call navUtils.navigateUpFromSameTask(this) method from activity A3 to call the same instance from the backstack instead of creating a new instance.

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(menuItem);
}

hmm...simple solution haa...:D ... Thanks guys for your help... !!




回答2:


Try getParentActivityIntent() instead of getParent()



来源:https://stackoverflow.com/questions/27255161/android-app-reacts-properly-to-to-phones-back-button-but-crashes-with-nulpointe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!