2012-04-22 20 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; 
     } 
    } 
} 

私はアプリを実行すると(makeMissedCallActivity.getmCallMadeFromApp() == true)場合、私はCallStateListener.java.

にラインでNullPointerExceptionが取得

何が問題なのでしょうか?

+1

例外の完全なスタックトレースを投稿できますか? – Templar

答えて

4

このコードブロックに問題があるようです。

MakeMissedCallActivity makeMissedCallActivity; 

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

あなたはプライベート変数を作成しているが、あなたのコンストラクタで、同じ名前を持つ新しいローカル変数を作成しているし、それに新しいアクティビティを割り当てます。コンストラクタを終了すると、プライベート変数はまだnullになります。

あなたがすることはおそらく単純です。

+0

ありがとうございます。それは本当に助けになりました。 – webgenius

関連する問題