gpt4 book ai didi

android - 当我在android中来电时启动Activity时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 16:51:59 25 4
gpt4 key购买 nike

我想在 Android 中来电时启动另一个 Activity 。以下是我在网站上找到的代码。该代码可以完美地在来电时弹出窗口,但是当我尝试通过传递 Intent (missintent)来启动 Activity 时,应用程序崩溃了。
注意:应用程序仅在来电时崩溃。当我在除来电之外的任何其他时间打电话时,我称之为我的错误的方法工作正常。

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)

最佳答案

似乎你实例化了类 CallHelper手动。因为它继承自 Activity你不应该那样做。 Activity 总是由框架启动。

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

关于android - 当我在android中来电时启动Activity时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28513821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com