2012-04-22 79 views
0

MakeMissedCallActivity.java:NullPointerException異常錯誤

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; 
     } 
    } 
} 

當我運行應用程序,我得到了行一個NullPointerException如果(makeMissedCallActivity.getmCallMadeFromApp() == true)CallStateListener.java.

任何想法可能是什麼問題?

+1

你能發佈異常的完整堆棧跟蹤嗎? – Templar 2012-04-22 05:33:30

回答

4

你的問題似乎出現在這段代碼中;

MakeMissedCallActivity makeMissedCallActivity; 

CallStateListener() { 
    MakeMissedCallActivity makeMissedCallActivity = new MakeMissedCallActivity(); 
} 

你正在創建一個私有變量,但在你的構造函數,你正在創建具有相同名稱一個新的局部變量和分配新的活動了這一點。當你退出構造函數時,私有變量仍然是null

你的意思是可能只是簡單的;

MakeMissedCallActivity makeMissedCallActivity; 

CallStateListener() { 
    makeMissedCallActivity = new MakeMissedCallActivity(); 
} 
+0

謝謝。它確實有幫助。 – webgenius 2012-04-22 05:51:47

相關問題