0

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

アプリを実行すると、行に nullPointerException が表示さ(makeMissedCallActivity.getmCallMadeFromApp() == true)れますCallStateListener.java.

何が問題なのですか?

4

1 に答える 1

4

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

MakeMissedCallActivity makeMissedCallActivity;

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

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

あなたがしようとしていることは、おそらく単純なことです。

MakeMissedCallActivity makeMissedCallActivity;

CallStateListener() {
    makeMissedCallActivity = new MakeMissedCallActivity();
}
于 2012-04-22T05:43:34.787 に答える