gpt4 book ai didi

android - Main Activity is not assignable android.app.Activity 错误

转载 作者:行者123 更新时间:2023-11-30 01:05:01 24 4
gpt4 key购买 nike

我是 Android 开发的新手。我在运行我的应用程序时遇到问题。我正在尝试使用 BroadcastReceiver 为来电通知干杯。但是我从 list com.sortinousn.Salesforce.calltracker is not assignable 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>

主要 Activity

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();

}
}
}
}

最佳答案

我真的不明白你为什么把MainActivity分类为 activity和一个 receiver ...这些不一样。

正如我在您的代码中看到的那样,您没有任何 Activity ,因为您扩展了 BroadcastReceiver在您的 MainActivity 类中...所以...通过使用 Activity 扩展另一个类来进行真正的 Activity (或其他)并将此类声明为 list 中的 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>

希望对你有帮助Darkball60.

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

或者您可以只删除 <activity />如果您不希望您的应用有任何界面,请在您的 list 中添加标签。

关于android - Main Activity is not assignable android.app.Activity 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38939429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com