Search code examples
javaandroidbuttonandroid-intentonactivityresult

App crashed when the return button is clicked


I'm trying to pass value k from Activity A to B. When the save button in the activity B is clicked, it will pass the value k to Activity A again. But the app crashed if I want to return back to A.

Some code snippet in Activity A

     public void onClick(DialogInterface dialog, int item) {
                    if (item == 0) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                       intent.putExtra("k",k);
                        startActivityForResult(intent, 0);
                    } else if (item == 1) {
                        Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                       intent.putExtra("k", k);
                        startActivityForResult(intent, 1);
                    }

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        int button = data.getIntExtra("k1", 0);
        if (button == 1) {
            switch (requestCode) {
                case 0:
                    String result = data.getStringExtra("text");
                    String b = data.getStringExtra("a");
                     as=Long.parseLong(result);
                    c.setText("            " + b + "------" + "RM " + result);
                    break;

                case 1:
                    String result1 = data.getStringExtra("text");
                    String b1 = data.getStringExtra("a");
                     as1=Long.parseLong(result1);
                    c.setText("            " + b1 + "------" + "RM " + result1);
                    break;
            }
        }
        else if(button==2)
        {
            switch (requestCode) {
                case 0:
                    String result = data.getStringExtra("text");
                    String b = data.getStringExtra("a");
                     bs=Long.parseLong(result);
                    d.setText("            " + b + "------" + "RM " + result);
                    break;

                case 1:
                    String result1 = data.getStringExtra("text");
                    String b1 = data.getStringExtra("a");
                     bs1=Long.parseLong(result1);
                    d.setText("            " + b1 + "------" + "RM " + result1);
                    break;
            }

        }

Activity B

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.project);
   txt= (EditText)findViewById(R.id.editText36);
    Button b=(Button)findViewById(R.id.button17);

    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            Intent returnIntent = new Intent();
            a="Project";
            text = txt.getText().toString();
            returnIntent.putExtra("text", text);
            returnIntent.putExtra("a", a);
            final int k1 = getIntent().getExtras().getInt("k");
            returnIntent.putExtra("k1", k1);
            setResult(Activity.RESULT_OK, returnIntent);
            finish();
        }
    });

}

@Override
public void onBackPressed()
{
    Intent intent = new Intent(Project1.this, Claims.class);
    startActivity(intent);
    finish();
}

LogCat Error

10-28 15:55:26.612    3641-3641/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 3641
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=2, result=0, data=null} to activity {com.example.project.project/com.example.project.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference

Latest error

 10-28 16:54:56.150    6381-6381/com.example.project.project E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.project.project, PID: 6381
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project.project/com.example.project.project.Claims}: java.lang.ClassCastException: com.example.project.project.Claims cannot be cast to android.app.Activity
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)

I did like what @we are borg suggested and declare the activity A (claims.java) in mainfest.xml, but get the error as above

Thanks everyone...I did like this and it solved :)

Activity A declare public static final int CAMERA_REQUEST_CODE = 6; add else if(requestCode==CAMERA_REQUEST_CODE) {}after else if(button==5){}

Activity B

Use

@Override
    public void onBackPressed()
    {
        Intent returnIntent = new Intent();
        setResult(Activity.RESULT_CANCELED, returnIntent);
        finish();
    }

instead of @Override

 public void onBackPressed()
    {
        Intent intent = new Intent(Project1.this, Claims.class);
        startActivity(intent);
        finish();
    }

Solution

  • Ok, shamelessly copy-pasting from the official docs at http://developer.android.com/training/basics/intents/result.html:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == PICK_CONTACT_REQUEST) {
            // Make sure the request was successful
            if (resultCode == RESULT_OK) {
                // The user picked a contact.
                // The Intent's data Uri identifies which contact was selected.
    
                // Do something with the contact here (bigger example below)
            }
        }
    }
    

    Make sure you have the if (resultCode == RESULT_OK) line in your code. If (resultCode == RESULT_CANCELED), then no result will be available and the Intent is null.

    When you press the "back" button on your device the normal result is the cancellation of the current activity, hence the calling activity gets the resultCode of RESULT_CANCELED in that case and not the one you set when explicitly returning via finish().