当我启动 Activity 而在 android 来电时应用程序崩溃
app crashes when i start Activity while incoming call in android
我想在 Android 来电时开始另一个 Activity。以下是我在网站上找到的代码。该代码非常适合在来电时弹出 window,但是当我尝试通过传递意图(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.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)
您似乎手动实例化了 class CallHelper
。因为它继承自 Activity
,所以你永远不应该那样做。活动始终由框架启动。
有两种可能性:要么 CallHelper
扮演 activity 的角色,然后让框架实例化它,要么不实例化它(这里似乎是这种情况)然后它不应继承自 Activity
.
我想在 Android 来电时开始另一个 Activity。以下是我在网站上找到的代码。该代码非常适合在来电时弹出 window,但是当我尝试通过传递意图(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.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)
您似乎手动实例化了 class CallHelper
。因为它继承自 Activity
,所以你永远不应该那样做。活动始终由框架启动。
有两种可能性:要么 CallHelper
扮演 activity 的角色,然后让框架实例化它,要么不实例化它(这里似乎是这种情况)然后它不应继承自 Activity
.