gpt4 book ai didi

java - 添加复选框后 Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 11:30:18 26 4
gpt4 key购买 nike

我正在创建一个调用阻止应用程序,当我在布局中添加复选框或单选按钮时,应用程序开始崩溃。

Activity 代码。

public void onCreate(Bundle savedInstanceState) {
//final EditText edittext = (EditText) findViewById(R.id.edittext);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=(EditText)findViewById(R.id.editText);
check=(CheckBox)findViewById(R.id.checkBox);

textview=(TextView)findViewById(R.id.textView);
String Number=edit.getText().toString();
String checked="0";
if(check.isChecked())
checked="1";
else
checked="0";

Intent intent = new Intent(PrefActivity.this, PhoneReceiver.class);
intent.putExtra("check", checked);
intent.putExtra("CELL", Number);
PendingIntent sender= PendingIntent.getBroadcast(
Activity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);



}


public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

}

广播接收器代码

public void onReceive(上下文上下文, Intent Intent ){

        String incomingNumber;
if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) return;
String extraState = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (extraState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

incomingNumber =intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

//SharedPreferences NUMBER=context.getSharedPreferences("myBlocker",0);

//String num=NUMBER.getString("yolo","yes");
//if (num.contentEquals(incomingNumber) /*|| incomingNumber.contentEquals("+923420050490") || incomingNumber.contentEquals("0342-0050490") || incomingNumber.contentEquals("+92331-7002407")*/) {
String num=intent.getStringExtra("CELL");
String check=intent.getStringExtra("check");
if(incomingNumber.contentEquals(num)) {
//SharedPreferences p = context.getSharedPreferences("myBlocker", 0);

// if (p.getBoolean("blockCalls", false)) {
if(check=="1")
terminateCall(context);
}
}
}

private void terminateCall(Context context) {
try {
Log.v(TAG, "Get getTeleService...");

Object telephonyService = getTelephonyServiceObject(context);

Class telephonyInterface = Class.forName("com.android.internal.telephony.ITelephony");


if (telephonyService != null && telephonyInterface != null) {
getAndInvokeMethod(telephonyInterface, telephonyService, "silenceRinger");
getAndInvokeMethod(telephonyInterface, telephonyService, "endCall");
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
}

private Object getTelephonyServiceObject(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
return m.invoke(tm);
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

private void getAndInvokeMethod(Class c, Object target, String methodName) {

try {
Method m = c.getMethod(methodName);
m.invoke(target);
} catch (Exception e) {
e.printStackTrace();
}
}

XML 文件如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Block This Number"
android:id="@+id/checkBox"
android:layout_below="@+id/editText"
android:layout_alignRight="@+id/editText"
android:layout_alignEnd="@+id/editText"
android:checked="false" />


</RelativeLayout>

错误日志如下所示

05-26 20:49:50.085: E/AndroidRuntime(15913): FATAL EXCEPTION: main
05-26 20:49:50.085: E/AndroidRuntime(15913): java.lang.RuntimeException: Unable to start receiver com.javaorigin.test.apk.PhoneReceiver: java.lang.NullPointerException
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2277)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.app.ActivityThread.access$1500(ActivityThread.java:140)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.os.Looper.loop(Looper.java:137)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.app.ActivityThread.main(ActivityThread.java:4898)
05-26 20:49:50.085: E/AndroidRuntime(15913): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 20:49:50.085: E/AndroidRuntime(15913): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 20:49:50.085: E/AndroidRuntime(15913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
05-26 20:49:50.085: E/AndroidRuntime(15913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
05-26 20:49:50.085: E/AndroidRuntime(15913): at dalvik.system.NativeStart.main(Native Method)
05-26 20:49:50.085: E/AndroidRuntime(15913): Caused by: java.lang.NullPointerException
05-26 20:49:50.085: E/AndroidRuntime(15913): at java.lang.String.contentEquals(String.java:1732)
05-26 20:49:50.085: E/AndroidRuntime(15913): at com.javaorigin.test.apk.PhoneReceiver.onReceive(PhoneReceiver.java:39)
05-26 20:49:50.085: E/AndroidRuntime(15913): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2270)
05-26 20:49:50.085: E/AndroidRuntime(15913): ... 10 more
05-26 20:49:50.150: E/android.os.Debug(2260): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
05-26 20:49:50.370: E/Bluetooth HS/HF(2818): handlePreciseCallStateChange() mPhone=RINGING, mFCall=IDLE, mBCall=IDLE, mRCall=INCOMING, mCallsetup=1, mCall=0, mCallheld=0
05-26 20:49:50.455: E/BargeInRecognizer(2818): stopBargeIn
05-26 20:50:00.120: E/dalvikvm(17755): Could not find class 'com.amazon.device.messaging.ADM', referenced from method com.whatsapp.App.aN
05-26 20:50:00.165: E/dalvikvm(17755): Could not find class 'com.amazon.device.messaging.ADM', referenced from method com.whatsapp.App.m
05-26 20:50:00.310: E/dalvikvm(17755): Could not find class 'com.whatsapp.adm.ADMMessageHandler', referenced from method com.whatsapp.iz.c
05-26 20:50:00.310: E/dalvikvm(17755): Could not find class 'com.whatsapp.adm.ADMMessageHandler', referenced from method com.whatsapp.iz.c
05-26 20:50:00.670: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:50:01.225: E/dalvikvm(17818): Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.hg.a
05-26 20:50:01.520: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:50:01.525: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:50:01.555: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:50:01.560: E/dalvikvm(17755): Could not find class 'com.whatsapp.j_', referenced from method com.whatsapp.xj.<clinit>
05-26 20:50:01.605: E/AuthorizationBluetoothService(13869): Proximity feature is not enabled.
05-26 20:50:01.890: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:50:07.760: E/ActivityThread(18347): Failed to find provider info for com.seven.provider.email
05-26 20:50:08.170: E/ActivityThread(18426): Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@424e4b38 that was originally bound here
05-26 20:50:08.170: E/ActivityThread(18426): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@424e4b38 that was originally bound here
05-26 20:50:08.170: E/ActivityThread(18426): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:965)
05-26 20:50:08.170: E/ActivityThread(18426): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:859)
05-26 20:50:08.170: E/ActivityThread(18426): at android.app.ContextImpl.bindService(ContextImpl.java:1344)
05-26 20:50:08.170: E/ActivityThread(18426): at android.app.ContextImpl.bindService(ContextImpl.java:1336)
05-26 20:50:08.170: E/ActivityThread(18426): at android.content.ContextWrapper.bindService(ContextWrapper.java:401)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:156)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:144)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.emailcommon.service.AccountServiceProxy.restoreAccountsIfNeeded(AccountServiceProxy.java:115)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.exchange.ExchangeService$11.run(ExchangeService.java:4020)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:792)
05-26 20:50:08.170: E/ActivityThread(18426): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:789)
05-26 20:50:08.170: E/ActivityThread(18426): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-26 20:50:08.170: E/ActivityThread(18426): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-26 20:50:08.170: E/ActivityThread(18426): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-26 20:50:08.170: E/ActivityThread(18426): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-26 20:50:08.170: E/ActivityThread(18426): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-26 20:50:08.170: E/ActivityThread(18426): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-26 20:50:08.170: E/ActivityThread(18426): at java.lang.Thread.run(Thread.java:856)
05-26 20:50:09.615: E/Watchdog(2260): !@Sync 2739
05-26 20:50:10.505: E/TinyUCM(1904): Normal is already enabled
05-26 20:50:10.550: E/Bluetooth HS/HF(2818): audioOff(): mPendingScoForA2dp: false, mPendingScoForWbs: false, mConnectedSco: null, mA2dpState: 11, mA2dpSuspended: false, mVoiceRecognitionStarted: false
05-26 20:50:10.670: E/ActivityThread(2818): Failed to find provider info for com.cequint.ecid
05-26 20:50:10.700: E/BargeInRecognizer(2818): stopBargeIn
05-26 20:50:10.755: E/Bluetooth HS/HF(2818): handlePreciseCallStateChange() mPhone=IDLE, mFCall=IDLE, mBCall=IDLE, mRCall=IDLE, mCallsetup=1, mCall=0, mCallheld=0
05-26 20:50:10.755: E/Bluetooth HS/HF(2818): audioOff(): mPendingScoForA2dp: false, mPendingScoForWbs: false, mConnectedSco: null, mA2dpState: 11, mA2dpSuspended: false, mVoiceRecognitionStarted: false
05-26 20:50:17.510: E/MtpService(10955): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
05-26 20:50:17.510: E/MtpService(10955): battPlugged Type : 2
05-26 20:50:35.270: E/dalvikvm(19024): Could not find class 'android.telephony.CellInfoCdma', referenced from method com.facebook.common.hardware.CellDiagnosticsSerializer.c
05-26 20:50:35.705: E/dalvikvm(19024): Could not find class 'com.facebook.push.adm.ADMBroadcastReceiver', referenced from method com.facebook.push.adm.ADMPushManager.b
05-26 20:50:39.620: E/Watchdog(2260): !@Sync 2740
05-26 20:50:48.505: E/MtpService(10955): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
05-26 20:50:48.505: E/MtpService(10955): battPlugged Type : 2
05-26 20:50:49.105: E/dalvikvm(19104): Could not find class 'android.telephony.CellInfoCdma', referenced from method com.facebook.common.hardware.j.c
05-26 20:50:49.190: E/dalvikvm(19133): Could not find class 'android.support.v4.app.FragmentActivity', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.b
05-26 20:50:49.195: E/dalvikvm(19133): Could not find class 'android.support.v4.app.FragmentActivity', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.b
05-26 20:50:49.250: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.275: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.315: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.345: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.355: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.365: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.395: E/GooglePlayServicesUtil(19133): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
05-26 20:50:49.755: E/dalvikvm(19104): Could not find class 'com.facebook.push.adm.ADMBroadcastReceiver', referenced from method com.facebook.push.adm.a.b
05-26 20:50:52.055: E/NotificationService(2260): Ignoring notification with icon==0: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 kind=[null]) tickerText=null contactCharSeq=null when=1432655452055 threadId=0
05-26 20:51:00.030: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:05.340: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:05.355: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:05.715: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:09.625: E/Watchdog(2260): !@Sync 2741
05-26 20:51:11.280: E/LockPatternKeyguardView(2260): mIsVoiceUnlockOn=false
05-26 20:51:11.960: E/CircleShortcutWidget(2260): density = 320
05-26 20:51:11.960: E/CircleShortcutWidget(2260): pkg name =com.android.contacts, activityName=com.android.contacts.activities.DialtactsActivity
05-26 20:51:12.060: E/CircleShortcutWidget(2260): pkg name =com.sec.chaton, activityName=com.sec.chaton.HomeActivity
05-26 20:51:12.145: E/CircleShortcutWidget(2260): pkg name =com.google.android.googlequicksearchbox, activityName=com.google.android.googlequicksearchbox.SearchActivity
05-26 20:51:12.375: E/CircleShortcutWidget(2260): pkg name =com.android.browser, activityName=com.android.browser.BrowserActivity
05-26 20:51:12.440: E/CircleShortcutWidget(2260): pkg name =com.sec.android.app.camera, activityName=com.sec.android.app.camera.Camera
05-26 20:51:12.800: E/WakeUpCmdRecognizer(2260): WakeUpCmdRecognizer Create
05-26 20:51:12.810: E/WakeUpCmdRecognizer(2260): setLanguage : en
05-26 20:51:12.810: E/WakeUpCmdRecognizer(2260): Country : US
05-26 20:51:12.810: E/WakeUpCmdRecognizer(2260): sVoiceLanguage : en-US
05-26 20:51:12.815: E/WakeUpCmdRecognizer(2260): isSamsungWakeUpLibExist : true
05-26 20:51:12.815: E/WakeUpCmdRecognizer(2260): isSensoryWakeUpLibExist : true
05-26 20:51:12.820: E/WakeUpCmdRecognizer(2260): init
05-26 20:51:12.820: E/VoiceEngineWrapper(2260): getInstance() : get existed VoiceEngine
05-26 20:51:12.820: E/SensoryEngineWrapper(2260): getInstance() : get existed SensoryJNI
05-26 20:51:12.820: E/VoiceEngine(2260): setIsRunningVoiceEngine mode : true
05-26 20:51:12.820: E/VoiceEngineWrapper(2260): getInstance() : get existed VoiceEngine
05-26 20:51:12.820: E/SensoryEngineWrapper(2260): getInstance() : get existed SensoryJNI
05-26 20:51:13.185: E/LocationReceiver(16720): Received bad location: null
05-26 20:51:13.835: E/KeyguardViewMediator(2260): 2. Lockscreen lock
05-26 20:51:13.835: E/KeyguardViewMediator(2260): Phone is boot completed. so can broadcast
05-26 20:51:14.670: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:19.790: E/MtpService(10955): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
05-26 20:51:19.790: E/MtpService(10955): battPlugged Type : 2
05-26 20:51:19.920: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:51:30.195: E/DBG_DM(19330): Warning!!! [v5_1209_1_2][Line:1813][xdmCheckSystemRooting] Device is ok
05-26 20:51:30.200: E/DBG_DM(19330): Warning!!! [v5_1209_1_2][Line:1733][xdmGetServiceManager] java.lang.NullPointerException
05-26 20:51:30.200: E/DBG_DM(19330): Warning!!! [v5_1209_1_2][Line:964][xdmGetTopActivityName] activityManager is null!!
05-26 20:51:39.625: E/Watchdog(2260): !@Sync 2742
05-26 20:51:43.390: E/WifiP2pStateTracker(2260): getNetworkInfo : NetworkInfo: type: WIFI_P2P[], state: UNKNOWN/IDLE, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true
05-26 20:51:43.565: E/DBG_DM(19330): Warning!!! [v5_1209_1_2][Line:1813][xdmCheckSystemRooting] Device is ok
05-26 20:51:53.795: E/MtpService(10955): In MTPAPP onReceive:android.intent.action.BATTERY_CHANGED
05-26 20:51:53.795: E/MtpService(10955): battPlugged Type : 2
05-26 20:52:00.820: E/MtpServerJNI(19368): could not open MTP driver, errno: 2
05-26 20:52:00.840: E/MtpServerJNI(19368): server is null in run
05-26 20:52:00.840: E/MtpServerJNI(19368): server is null in cleanup
05-26 20:52:05.700: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:52:09.625: E/Watchdog(2260): !@Sync 2743
05-26 20:52:10.760: E/WifiHW(2260): ##################### set firmware type 0 #####################
05-26 20:52:21.320: E/ResourceType(19403): getSupportListinCSC: cannot open file
05-26 20:52:21.320: E/ResourceType(19403): The locale in CSC is empty.

最佳答案

看起来这行代码导致了异常:

if(incomingNumber.contentEquals(num)) {

传入的Number为null,而null没有contentEquals方法,所以会抛出异常。

因此,您需要在发送之前将键 TelephonyManager.EXTRA_INCOMING_NUMBER 的字符串值传递给 Intent。

代码中还有一个问题:

if(check=="1")

应更改为 if("1".contentEquals(check))

关于java - 添加复选框后 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30455409/

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