gpt4 book ai didi

java.lang.RuntimeException : Unable to instantiate service 错误

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:56 24 4
gpt4 key购买 nike

我正在尝试编写一个监控短信的应用程序。我想从我的主 Activity 类启动一个服务,但由于某种原因该服务没有启动。我认为我如何在 list 文件中声明我的服务或我如何从我的 Activity 中调用它可能存在问题。这是我的 Activity 代码的一部分:

public class TablighBlockActivity extends Activity {
/** Called when the activity is first created. */
private ToggleButton toggEnable;

public void onCreate(Bundle savedInstanceState) {
if (loadState("running")==false){startService(new Intent(this, SMSMonitor.class));}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

这是我的服务类的一部分:

public class SMSMonitor extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

Intent i = new Intent(context,SMSMonitor.class);
i.setClass(context, SMSMonitor.class);
context.startService(i);

这是我的 list 文件:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tabligh.block"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name="SMSMonitor">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>

</receiver>

<activity
android:name=".TablighBlockActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".SMSMonitor"
android:process=":smsmonitor"
></service>
</application>

</manifest>

这是错误日志:

06-22 13:46:14.649: W/dalvikvm(1013): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
06-22 13:46:14.668: E/AndroidRuntime(1013): Uncaught handler: thread main exiting due to uncaught exception
06-22 13:46:15.038: E/AndroidRuntime(1013): java.lang.RuntimeException: Unable to instantiate service tabligh.block.SMSMonitor: java.lang.ClassCastException: tabligh.block.SMSMonitor
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2449)
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.app.ActivityThread.access$2800(ActivityThread.java:112)
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1744)
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.os.Looper.loop(Looper.java:123)
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.app.ActivityThread.main(ActivityThread.java:3948)
06-22 13:46:15.038: E/AndroidRuntime(1013): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 13:46:15.038: E/AndroidRuntime(1013): at java.lang.reflect.Method.invoke(Method.java:521)
06-22 13:46:15.038: E/AndroidRuntime(1013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
06-22 13:46:15.038: E/AndroidRuntime(1013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
06-22 13:46:15.038: E/AndroidRuntime(1013): at dalvik.system.NativeStart.main(Native Method)
06-22 13:46:15.038: E/AndroidRuntime(1013): Caused by: java.lang.ClassCastException: tabligh.block.SMSMonitor
06-22 13:46:15.038: E/AndroidRuntime(1013): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2446)
06-22 13:46:15.038: E/AndroidRuntime(1013): ... 10 more

谢谢。

最佳答案

   public class SMSMonitor extends BroadcastReceiver {

你是 SMSMonitor,它是 BroadcastReceiver 而不是服务

Intent i = new Intent(context,SMSMonitor.class);
i.setClass(context, SMSMonitor.class);
context.startService(i);

关于java.lang.RuntimeException : Unable to instantiate service 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11157953/

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