Sara
Sara

Reputation: 509

NullPointerException when trying to get data out of intent in onActivityResult

I'm new to Android programming and I have an activity that has an imageButton, when it is clicked, it opens a new activity for results with dialog theme. The user enters some data and clicks submit. I then need to get that data out of the intent to update some images on the first activity. The problem is that I keep getting a NullPointerException when I try to access the string in the intent.

1st activity's onclick method:

public void openDetails(View view){

        Intent intent = new Intent(this, FinalAnalysisDialog.class);
        //add to backStack
        startActivityForResult(intent, 1);

    }

2nd activity (dialog themed):

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.final_analysis_activity);

        WindowManager.LayoutParams params = getWindow().getAttributes();  
        //params.x = -100;  
        params.height = 500;  
        params.width = 600;  
        //params.y = -50;  

        this.getWindow().setAttributes(params);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //final??
        final Spinner dropDown = (Spinner) findViewById(R.id.faResultsDropDown);
        dropDown.setSelection(2);

        Button submit = (Button) findViewById(R.id.faSubmitButton);
        submit.setOnClickListener(new OnClickListener() {
            //based on the value of selected item from dropDown (spinner), update the
            //progress status picture
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                //dropDown.getSelectedItemPosition();
                String result = dropDown.getSelectedItem().toString();
                //String result = "In process";
                bundle.putString("result", result);

                Intent intent = new Intent();
                //intent.putExtras(bundle);
                intent.putExtra("result", result);
                setResult(RESULT_OK, intent);
        finish();
            }
        });

Then I try to get the string back out:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK && requestCode==1){
            //ImageButton mFApic = (ImageButton)home.getView().findViewById(R.id.mInsStepImage);//andrology_home_fragment

            ImageButton mFApic = (ImageButton)findViewById(R.id.mInsStepImage);//andrology_home_fragment
            View mHomeFApic = (View)findViewById(R.id.maleFA);//male_details
            //do something with result
            //Intent dataResult = data.getData();
            //Bundle bundle =dataResult.getExtras();
            //String result = bundle.getString("result");

            Bundle extras = data.getExtras();
            String result = extras.getString("result");
            //String result = data.getStringExtra();
            if (result.equals("Not started")){
                mFApic.setBackgroundResource(R.drawable.orange_process);
                mHomeFApic.setBackgroundResource(R.drawable.orange_process);
            }else if (result.equals("In process")){
                mFApic.setBackgroundResource(R.drawable.orange_process);
                mHomeFApic.setBackgroundResource(R.drawable.orange_process);
            }else if (result.equals("Complete")){
                mFApic.setBackgroundResource(R.drawable.green_process);
                mHomeFApic.setBackgroundResource(R.drawable.green_process);
            }else if (result.equals("Problem")){
                mFApic.setBackgroundResource(R.drawable.red_process);
                mHomeFApic.setBackgroundResource(R.drawable.red_process);
            }
        }

I've tried to send a static string "In process" to see if the problem might be getting value out of spinner but that didn't work. I've searched similar questions but I can't seem to get this to work. What am I missing? Any help would be greatly appreciated..

Here's my logCat:

05-09 14:26:31.133: E/AndroidRuntime(4984): FATAL EXCEPTION: main
05-09 14:26:31.133: E/AndroidRuntime(4984): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.seattleivf/com.example.seattleivf.TabActionBarHomeActivity}: java.lang.NullPointerException
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread.access$1100(ActivityThread.java:140)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.os.Looper.loop(Looper.java:137)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread.main(ActivityThread.java:4895)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at java.lang.reflect.Method.invoke(Method.java:511)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at dalvik.system.NativeStart.main(Native Method)
05-09 14:26:31.133: E/AndroidRuntime(4984): Caused by: java.lang.NullPointerException
05-09 14:26:31.133: E/AndroidRuntime(4984):     at com.example.seattleivf.TabActionBarHomeActivity.onActivityResult(TabActionBarHomeActivity.java:197)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.Activity.dispatchActivityResult(Activity.java:5347)
05-09 14:26:31.133: E/AndroidRuntime(4984):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
05-09 14:26:31.133: E/AndroidRuntime(4984):     ... 11 more

Upvotes: 1

Views: 2187

Answers (1)

Daniel Conde Marin
Daniel Conde Marin

Reputation: 7742

You r problem is that you are retrieving the String from the bundle and no the intent itself, try using:

String result = data.getStringExtra("result");

or simply uncomment the line:

//intent.putExtras(bundle);

and retrieve the String as initially you tried to.

Upvotes: 4

Related Questions