我的应用程序在启动时崩溃并出现以下错误。
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
代码
MyPhoneCall.java 包 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>