Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序未启动,出现致命异常_Android_Android Launcher - Fatal编程技术网

Android 应用程序未启动,出现致命异常

Android 应用程序未启动,出现致命异常,android,android-launcher,Android,Android Launcher,我的应用程序在启动时崩溃,出现以下错误 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

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

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> 


您试图将该类作为活动启动的地方。它不是——它是一个接收器。活动是一个具有UI的工作流,该UI可以接受用户输入并保持手机的前景。BroadcastReceiver是一段短暂的代码,它对事件做出响应,然后消失。它没有UI,不与用户交互,并且快速退出。基于这一点和前面的问题,我认为您不理解它们。

在清单中,您已将MyPhoneCall声明为活动。但是在代码中,您已经使用广播接收器扩展了MyPhoneCall类。

因为MyPhoneCall类没有扩展活动类,所以出现此错误。不要将其放入活动块中。只需在menifest中的Receiver块中注册,您的
MyPhoneCall
就是一个
BroadcastReceiver
,您可以将其声明为一个
活动。所以它给了你错误

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>


MyPhoneCall.java是一种
广播接收器
。它不是
活动
。但是在您的
AndroidManifest.xml
中,您已经将其声明为
活动。因此,您会收到一个
ClassCastException

我认为您的活动和接收器是反向的,因为您确实定义了MyPhoneReceiver,但您将BroadcastReceiver应用于MyPhoneCall类。这个类应该像答案显示的那样在接收者中定义。哪个类有你的实际活动,因为我在你的MyPhoneCall类中没有看到它。。。