我的应用程序在启动时出现以下错误而崩溃。
05-13 05:55:33.031: E/AndroidRuntime(1022): FATAL EXCEPTION: main
05-13 05:55:33.031: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myphonecall/com.example.myphonecall.MyPhoneCall}: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.os.Looper.loop(Looper.java:137)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-13 05:55:33.031: E/AndroidRuntime(1022): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 05:55:33.031: E/AndroidRuntime(1022): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 05:55:33.031: E/AndroidRuntime(1022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-13 05:55:33.031: E/AndroidRuntime(1022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-13 05:55:33.031: E/AndroidRuntime(1022): at dalvik.system.NativeStart.main(Native Method)
05-13 05:55:33.031: E/AndroidRuntime(1022): Caused by: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-13 05:55:33.031: E/AndroidRuntime(1022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-13 05:55:33.031: E/AndroidRuntime(1022): ... 11 more
CODE
MyPhoneCall.java package com.example.myphonecall;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class MyPhoneCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("MY_DEBUG_TAG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//Log.w("MY_DEBUG_TAG", phoneNumber);
Toast.makeText(context, "Getting Call from "+phoneNumber, Toast.LENGTH_LONG).show();
}
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myphonecall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyPhoneCall"
android:label="MyPhoneCall" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:2)
您的MyPhoneCall
是BroadcastReceiver
并且您将其声明为activity
。所以它给你错误。
05-13 05:55:33.031: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myphonecall/com.example.myphonecall.MyPhoneCall}: java.lang.ClassCastException: com.example.myphonecall.MyPhoneCall cannot be cast to android.app.Activity
按以下方式声明。
<receiver android:name=".MyPhoneCall" >
<intent-filter>
<action ...... />
<action ..... />
</intent-filter>
</receiver>
答案 1 :(得分:1)
MyPhoneCall.java 是BroadcastReceiver
。它不是Activity
。但是在AndroidManifest.xml
中,您已将其声明为Activity
。因此,您会收到ClassCastException
。
答案 2 :(得分:0)
某处您尝试将该类作为活动启动。它不是 - 它的接收器。活动是具有UI的工作流,该UI可以接收用户输入并保持电话的前景。 BroadcastReceiver是一段短暂的代码,它响应事件然后消失。它没有UI,不与用户交互,并且快速退出。基于这个和前一个问题,我认为你不理解它们。
答案 3 :(得分:0)
在Manifest中,您已将MyPhoneCall声明为活动。但是在代码中,您已经使用广播接收器扩展了MyPhoneCall类。
答案 4 :(得分:0)
因为My Phone Call类没有扩展Activity类,这就是为什么会出现这个错误。不要放入活动块。只需在清单
中的Receiver块中注册即可