【问题标题】:Main Activity is not assignable android.app.Activity error主活动不可分配 android.app.Activity 错误
【发布时间】:2016-12-20 17:38:16
【问题描述】:

我对 Android 开发非常陌生。我在运行我的应用程序时遇到问题。我正在尝试使用广播接收器为来电通知祝酒。但我从清单 com.sortinousn.Salesforce.calltracker is notassignable to android.app.Activity 中收到此错误

AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sortinousn.salesforcecalltracker.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>


</application>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-    permission>

</manifest>

主活动

public class MainActivity extends BroadcastReceiver {

Context pcontext;

public void onReceive(Context context, Intent intent) {

    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        //Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {

        Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

        if (state == 1) {

            String msg = "New Phone Call Event. Incomming Number :     "+incomingNumber;
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(pcontext, msg, duration);
            toast.show();

        }
    }
}
}

【问题讨论】:

  • 您必须在清单中添加所有活动。如果我没记错的话,您没有在清单中添加 calltracker 活动。尝试一下
  • MainActivity 是一个接收者,所以你不能在&lt;activity&gt; 标签的android:name 属性中声明它

标签: android android-studio android-activity android-manifest


【解决方案1】:

我真的不明白你为什么将MainActivity 类作为activityreceiver...这些不一样。

正如我在您的代码中看到的那样,您没有任何活动,因为您在 MainActivity 类中扩展了BroadcastReceiver...所以...只需通过使用Activity 扩展另一个类来进行真正的活动(或其他人)并将此类声明为清单中的活动,就像这样:Activity

package fr.zwedge.kingwarrior;

import android.app.*;
import android.os.Bundle;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Tell me if I'm wrong for what's above
        BroadcastReceiver receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                try {
                    // TELEPHONY MANAGER class object to register one listner
                    TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

                    //Create Listner
                    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

                   // Register listener for LISTEN_CALL_STATE
                   tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

               } catch (Exception e) {
                   Log.e("Phone Receive Error", " " + e);
               }
           }
       }
   }
   public /* can't be private here */ class MyPhoneStateListener extends PhoneStateListener {
      public void onCallStateChanged(int state, String incomingNumber) {
         Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

         if (state == 1) {
            String msg = "New Phone Call Event. Incomming Number :     "+incomingNumber;
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(pcontext, msg, duration);
            toast.show();
         }
      }
  }

Manifest

<application android:allowBackup="true"
    android:label="King Warrior"
    android:icon="@drawable/ic_launch">
    <activity android:name=".Main"
        android:label="What you want"
        android:icon="@drawable/ic_launch">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

希望对你有帮助, 黑球60。

===================================

或者,如果您不想为您的应用提供任何界面,您也可以删除清单中的 &lt;activity /&gt; 标记。

【讨论】:

    【解决方案2】:

    你不能为一个activity声明一个receiver,你必须在你的activity之外创建你的receiver并在你的MainActivity中回调这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多