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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

That sounded promising, but has the same result :(
@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
bCall.setOnClickListener(new CallClickListener(getApplicationContext())); should be YourActivityClass.this instead of getApplicationContext. Try that.
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
@SamarthJain i did suggest the same before but was not sure. so edited my answer again. coz he says alertdialog works
|
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();
        }

    });

9 Comments

Thnaks for the quick reply. I Obviously assign the button to a resource first. But when i try your code, i get the error 'CallClickListener' is not an enclosing class
I use the application context to get the application prefferences
@M4tchB0X3r i am checking again.
Yes i have. Like i said it used to work. but since implementing the dynamic strings it crashes
@M4tchB0X3r log this contactPersonTelephone and check whether this is null or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.