接收器中的致命异常

时间:2015-10-09 06:35:20

标签: android eclipse fatal-error

这是扩展BroadcastReceiver的接收器代码。每次我安装它并且电话铃响它突然停止并发出此错误。我在这里找到了这段代码How to detect incoming calls, in an Android device?

 public abstract   class PhonecallReceiver extends BroadcastReceiver {

//The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations
static PhonecallStartEndDetector listener;
String outgoingSavedNumber;
protected Context savedContext;


@Override
public void onReceive(Context context, Intent intent) {
    savedContext = context;
    if(listener == null){
        listener = new PhonecallStartEndDetector();
    }

    //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
        listener.setOutgoingNumber(intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"));
        return;
    }

    //The other intent tells us the phone state changed.  Here we set a listener to deal with it
    TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}

//Derived classes should override these to respond to specific events of interest
protected abstract void onIncomingCallStarted(String number, Date start);
protected abstract void onOutgoingCallStarted(String number, Date start);
protected abstract void onIncomingCallEnded(String number, Date start, Date end); 
protected abstract void onOutgoingCallEnded(String number, Date start, Date end);
protected abstract void onMissedCall(String number, Date start);

  //Deals with actual events
  public class PhonecallStartEndDetector extends PhoneStateListener {
    int lastState = TelephonyManager.CALL_STATE_IDLE;
    Date callStartTime;
    boolean isIncoming;
    String savedNumber;  //because the passed incoming is only valid in ringing

    public PhonecallStartEndDetector() {}

    //The outgoing number is only sent via a separate intent, so we need to store it out of band
    public void setOutgoingNumber(String number){
        savedNumber = number;
    }

    //Incoming call-  goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
    //Outgoing call-  goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        if(lastState == state){

            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                callStartTime = new Date();
                savedNumber = incomingNumber;
                onIncomingCallStarted(incomingNumber, callStartTime);
                Log.i("", "2");

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing donw on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                    callStartTime = new Date();
                    onOutgoingCallStarted(savedNumber, callStartTime);  
                    Log.i("", "3");

                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(savedNumber, callStartTime);
                    Log.i("", "4");

                }
                else if(isIncoming){
                    onIncomingCallEnded(savedNumber, callStartTime, new Date()); 
                    Log.i("", "5");

                }
                else{
                    onOutgoingCallEnded(savedNumber, callStartTime, new Date());    
                    Log.i("", "6");

                }
                break;
        }
        lastState = state;
    }

}



 }

错误:

10-09 09:51:08.656: E/AndroidRuntime(4249): FATAL EXCEPTION: main
10-09 09:51:08.656: E/AndroidRuntime(4249): java.lang.RuntimeException:     Unable to instantiate receiver android.call.PhonecallReceiver:     java.lang.InstantiationException: can't instantiate class    android.call.PhonecallReceiver
10-09 09:51:08.656: E/AndroidRuntime(4249):     at     android.app.ActivityThread.handleReceiver(ActivityThread.java:2460)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at     android.app.ActivityThread.access$1500(ActivityThread.java:150)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1374)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at android.os.Looper.loop(Looper.java:213)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at android.app.ActivityThread.main(ActivityThread.java:5225)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at java.lang.reflect.Method.invoke(Method.java:525)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at     dalvik.system.NativeStart.main(Native Method)
10-09 09:51:08.656: E/AndroidRuntime(4249): Caused by: java.lang.InstantiationException: can't instantiate class    android.call.PhonecallReceiver
10-09 09:51:08.656: E/AndroidRuntime(4249):     at     java.lang.Class.newInstanceImpl(Native Method)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at java.lang.Class.newInstance(Class.java:1130)
10-09 09:51:08.656: E/AndroidRuntime(4249):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2455)
10-09 09:51:08.656: E/AndroidRuntime(4249):     ... 10 more

1 个答案:

答案 0 :(得分:1)

您无法实例化抽象类。您必须使其成为普通公共类,并在清单文件中定义它。 希望能起作用