Skip to content Skip to sidebar Skip to footer

Android Call Intent Null Pointer Exception

Im at my wits end here. I have a Class which Implements the OnClickListener cous i need the same action on Buttons accros my Application. This used to work just fine. But since I a

Solution 1:

Do this.... make the context object that you passed in the constructor into a field variable. and change startActivity to context.startActivity. It will work then.

EDIT: Highlighting the full solution.

bCall.setOnClickListener(new CallClickListener(getApplicationContext()));

should be changed toYourActivityClass.this instead of getApplicationContext.

Start Activity in the same task does not work with a context object that is not an Activity. So you need to either change the context to Activity or you start the activity in a new task. Also without calling startActivity on the context provided to your constructor you were getting the NPE because your CallClickListerner has no context.

Solution 2:

Use activity context. Also check if you have initialized bCall. If you have not you will get NullPointerException.

     bCall.setOnClickListener(ActivityName.this);

Also check this link to know when to use activity context and when to use application context

When to call activity context OR application context?

Edit:

Make sure you have added permission in manifest file

<uses-permissionandroid:name="android.permission.CALL_PHONE" />

For reference use the below. My Class extends Activity

   Button b= (Button) findViewById(R.id.button1); 
   b.setOnClickListener(newOnClickListener()
    {

        @OverridepublicvoidonClick(View v1) {
            // TODO Auto-generated method stubfinalViewv= v1;

                AlertDialog.Builderalert=newAlertDialog.Builder(v.getContext());
                alert.setTitle("Anrufen");
                alert.setMessage("Kontakt für " );
                alert.setPositiveButton("Anrufen", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
                        IntentcallIntent=newIntent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:8095992052"));
                        startActivity(callIntent);// this line throws the exception
                    }
                });
                alert.setNegativeButton("Abbrechen", newDialogInterface.OnClickListener() {
                    @OverridepublicvoidonClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show();
        }

    });

Post a Comment for "Android Call Intent Null Pointer Exception"