0

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 added some functionality by getting some needed data from the app preferences. startActivity throws a null pointer exception.
Here is the class:

//Imports
public class CallClickListener extends Activity implements View.OnClickListener {

    protected AppPreferences appPrefs;
    String contactPersonName;
    String contactPersonTelephone;
    String name;

    public CallClickListener(Context context){
        Log.d("TRACE", "init CallClick");
        appPrefs = new AppPreferences(context);

        try {

            JSONObject object = appPrefs.getConsultantObject();

            contactPersonName = object.getString("contactPersonName");
            contactPersonTelephone = object.getString("contactPersonTelephone");
            name = object.getString("name");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        final View v = view;

        AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
        alert.setTitle("Anrufen");
        alert.setMessage("Kontakt für " + name + ", " + contactPersonName + " anrufen");
        alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:"+contactPersonTelephone));
                startActivity(callIntent);// this line throws the exception
            }
        });
        alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
            }
        });
        alert.show();
    }
}

The Strings are all there from appPrefs, i also tried with hardcoding a phonenumber just incase. the Alert works fine, but as soon as i hit the positive Button, the app crashes
I add the Listener like this:

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

I added the necessary Call permissions.

I'm fairly new to Android dev, what am I missing?

M4tchB0X3r
  • 1,531
  • 1
  • 15
  • 28

2 Answers2

1

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-permission android:name="android.permission.CALL_PHONE" />

For reference use the below. My Class extends Activity

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

        @Override
        public void onClick(View v1) {
            // TODO Auto-generated method stub
              final View v = v1;

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

    });
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
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 to YourActivityClass.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.

Sam
  • 3,413
  • 1
  • 18
  • 20
  • That sounded promising, but has the same result :( – M4tchB0X3r May 25 '13 at 09:37
  • @M4tchB0X3r i just tried your code by harcoding the number. the alertdialog works. on clicking the button it does make a call. i am not sure why you are getting NPE – Raghunandan May 25 '13 at 09:39
  • bCall.setOnClickListener(new CallClickListener(getApplicationContext())); should be YourActivityClass.this instead of getApplicationContext. Try that. – Sam May 25 '13 at 09:40
  • srym not quite the same, now i gget this error! "Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?" not sure what that means – M4tchB0X3r May 25 '13 at 09:40
  • @SamarthJain i did suggest the same before but was not sure. so edited my answer again. coz he says alertdialog works – Raghunandan May 25 '13 at 09:41
  • Yes ... that's what i was looking for... make the edit i told you in the previous comment and it'll work. 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. – Sam May 25 '13 at 09:42
  • @M4tchB0X3r i used a button with annonymous inner class and on button click used you alertdialog and the call works fine. i harcoded the number. You can check the edit. it works for me – Raghunandan May 25 '13 at 09:47
  • got it working thx. the hint with YourActivityClass.this did it. CallClickListener.this as Raghunandan suggested obviously didnt work since thats the listener im adding. – M4tchB0X3r May 25 '13 at 09:50
  • Good to hear it works. But going forward you must understand why that error happened. – Sam May 25 '13 at 09:52
  • I do understand, thats why the answer sounded promising to start off with. I needed to pass the ativity context not application context, and start the intent over that – M4tchB0X3r May 25 '13 at 09:55
  • great... off to the next problem then :D – Sam May 25 '13 at 09:57
  • Lets hope not ;) *fingers crossed* – M4tchB0X3r May 25 '13 at 09:58