-1

我的应用程序在启动时崩溃并出现以下错误。

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> 
4

5 回答 5

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>
于 2013-05-13T06:11:54.683 回答
1

MyPhoneCall.java 是一个BroadcastReceiver. 它不是一个Activity. 但是在您的中,AndroidManifest.xml您已将其声明为Activity. 因此,您收到一个ClassCastException.

于 2013-05-13T06:11:59.293 回答
0

在某个地方,您正试图将该课程作为一项活动启动。它不是——它是一个接收器。Activity 是一个带有 UI 的工作流,它可以接受用户输入并保持手机的前景。BroadcastReceiver 是一段短暂的代码,它响应事件然后消失。它没有 UI,不与用户交互,并且快速退出。基于this和previous question,我认为您不了解它们。

于 2013-05-13T06:10:33.907 回答
0

在 Manifest 中,您已将 MyPhoneCall 声明为 activity 。但是在代码中,您使用广播接收器扩展了 MyPhoneCall 类。

于 2013-05-13T06:11:13.460 回答
0

因为 MyPhoneCall 类没有扩展 Activity 类那为什么会出现这个错误。不要放入活动块。只需在清单中的 Receiver 块中注册

于 2013-05-13T06:11:19.877 回答