主Activity不能分配android.app.Activity错误

时间:2016-08-14 05:26:17

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

我对Android开发很新。我在运行我的应用时出现问题。我正试图通过使用BroadcastReceiver为来电通知祝酒。但我从清单 com.sortinousn.Salesforce.calltracker得到这个错误不能分配给android.app.Activity

的AndroidManifest.xml

document.write("-10.0/0 is " + (-10.0/0) + '<br>');

MainActivity

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

1 个答案:

答案 0 :(得分:1)

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

正如我在您的代码中看到的那样,您没有任何活动,因为您在MainActivity类中扩展BroadcastReceiver ...所以......只需通过{扩展另一个类来创建一个真正的活动{1}}(或其他人)并将此类声明为清单中的活动,如下所示: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

希望它有所帮助, Darkball60。

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

或者,如果您不想要应用的任何界面,则可以删除清单中的<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> 标记。