MakeMissedCallActivity.java:
package com.android.main;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Bundle;
import android.util.Log;
public class MakeMissedCallActivity extends Activity {
private Button button;
private static boolean mCallMadeFromApp = false;
private String LOG_TAG = "MakeMissedCall App";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Listen for call state changes before making the call through button
CallStateListener callStateListener = new CallStateListener();
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
// add button and add dial functionality
button = (Button) findViewById(R.id.buttonCall);
button.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View arg0) {
mCallMadeFromApp = true;
Log.i(LOG_TAG, "Button clicked");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+918028563681"));
startActivity(callIntent);
}
});
}
public boolean getmCallMadeFromApp() {
Log.i(LOG_TAG, "mCallMadeFromApp=" +mCallMadeFromApp);
return mCallMadeFromApp;
}
public void setmCallMadeFromApp(boolean mNewValue) {
mCallMadeFromApp = mNewValue;
}
}
CallStateListener.java:
package com.android.main;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
//monitor phone call activities
public class CallStateListener extends PhoneStateListener {
private String LOG_TAG = "MakeMissedCall App";
MakeMissedCallActivity makeMissedCallActivity;
CallStateListener() {
MakeMissedCallActivity makeMissedCallActivity = new MakeMissedCallActivity();
}
@Override
public void onCallStateChanged(int call_state, String incomingNumber) {
switch(call_state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.i(LOG_TAG, "CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "CALL_STATE_OFFHOOK. mCallMadeFromApp=" + makeMissedCallActivity.getmCallMadeFromApp());
break;
case TelephonyManager.CALL_STATE_IDLE:
if (makeMissedCallActivity.getmCallMadeFromApp() == true) {
makeMissedCallActivity.setmCallMadeFromApp(false);
}
Log.i(LOG_TAG, "CALL_STATE_IDLE");
break;
}
}
}
When I run the app, I get a nullPointerException in the line if (makeMissedCallActivity.getmCallMadeFromApp() == true) in CallStateListener.java.
Any idea what may be the problem?
Your problem seems to be in this block of code;
You’re creating a private variable, but in your constructor, you’re creating a new local variable with the same name and assign the new activity to that. When you exit the constructor, the private variable will still be
null.What you mean to do is probably just simply;