应用程序崩溃,当我在Android中的来电时启动Activity

时间:2015-02-14 08:26:23

标签: android eclipse crash

我想在Android中接听来电时启动另一个活动。以下是我在网站上找到的代码。该代码非常适合在传入呼叫时弹出窗口,但是当我尝试通过传递intent(missintent)来启动Activity时,应用程序崩溃了。 注意:应用仅在来电时崩溃。当我在来电以外的任何其他时间打电话时,我称之为missintent的方法工作正常。

package com.bitgriff.androidcalls;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

/**
 * Helper class to detect incoming and outgoing calls.
 * @author Moskvichev Andrey V.
 *
 */
public class CallHelper extends Activity{

    /**
     * Listener to detect incoming calls. 
     */
    private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(ctx, 
                        "Incoming: "+incomingNumber, 
                        Toast.LENGTH_LONG).show();
                //app crashes due to following 3 lines. Above toast works perfectly
                Intent missintent= new Intent(ctx, LEDOnOff.class); 
                missintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(missintent);
                break;
            }
        }
    }

    /**
     * Broadcast receiver to detect the outgoing calls.
     */
    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

            Toast.makeText(ctx, 
                    "Outgoing: "+number, 
                    Toast.LENGTH_LONG).show();
        }

    }

    private Context ctx;
    private TelephonyManager tm;
    private CallStateListener callStateListener;

    private OutgoingReceiver outgoingReceiver;

    public CallHelper(Context ctx) {
        this.ctx = ctx;

        callStateListener = new CallStateListener();
        outgoingReceiver = new OutgoingReceiver();
    }

    /**
     * Start calls detection.
     */
    public void start() {
        tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        ctx.registerReceiver(outgoingReceiver, intentFilter);
    }

    /**
     * Stop calls detection.
     */
    public void stop() {
        tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        ctx.unregisterReceiver(outgoingReceiver);
    }

}

显示以下logcat错误:

02-14 14:10:10.155: D/AndroidRuntime(29294): Shutting down VM
02-14 14:10:10.155: W/dalvikvm(29294): threadid=1: thread exiting with uncaught exception (group=0x4181eda0)
02-14 14:10:10.165: E/AndroidRuntime(29294): FATAL EXCEPTION: main
02-14 14:10:10.165: E/AndroidRuntime(29294): Process: com.bitgriff.androidcalls, PID: 29294
02-14 14:10:10.165: E/AndroidRuntime(29294): java.lang.NullPointerException
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.app.Activity.startActivityForResult(Activity.java:3584)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.app.Activity.startActivityForResult(Activity.java:3545)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.app.Activity.startActivity(Activity.java:3787)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.app.Activity.startActivity(Activity.java:3755)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at com.bitgriff.androidcalls.CallHelper$CallStateListener.onCallStateChanged(CallHelper.java:35)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:461)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.os.Looper.loop(Looper.java:146)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at android.app.ActivityThread.main(ActivityThread.java:5653)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at java.lang.reflect.Method.invokeNative(Native Method)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at java.lang.reflect.Method.invoke(Method.java:515)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
02-14 14:10:10.165: E/AndroidRuntime(29294):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

似乎您手动实例化了类CallHelper。由于它继承自Activity,你永远不应该这样做。活动总是由框架开始。

有两种可能性:CallHelper扮演一个活动的角色,然后让框架实例化它或者它不会(这似乎是这里的情况)然后它不应该继承Activity