簡體   English   中英

對於服務類,我看到 android.content.ActivityNotFoundException: Unable to find explicit activity class

[英]For a Service class I see android.content.ActivityNotFoundException: Unable to find explicit activity class

我正在使用 Android 並在創建服務並從主活動線程運行它時遇到以下錯誤。 我確實瀏覽了與此錯誤相關的所有線程,但他們看到的錯誤主要是針對 Activity 任務。 以下是錯誤和代碼的詳細信息

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.serviceexample/com.example.serviceexample.MyServiceBg}; have you declared this activity in your AndroidManifest.xml?

我確實檢查了我的 AndriodManifest.xml,我確實看到了聲明的服務

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serviceexample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="ServiceExample"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service
            android:name=".MyIntentService"
            android:exported="false"></service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <service android:name=".MyBgService"/>
    </application>

</manifest>

主活動.java:

package com.example.serviceexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button)findViewById(R.id.displayBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent displayTost  = new Intent(MainActivity.this, MyBgService.class);
                startActivity(displayTost);
            }
        });
    }

我的后台服務:

package com.example.serviceexample;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class MyBgService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int i;
        for (i = 1; i< 10; i++) {
            Toast.makeText(getApplicationContext(), "Number = "+i, Toast.LENGTH_LONG).show();
        }
        return Service.START_NOT_STICKY;
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

我確實嘗試在 AndroidManifest.xml 中鏈接路徑。 但是,仍然出現相同的錯誤。 不知道是什么問題。

這是 Gradle 的版本: 3.5.3 ,我使用的是 SDK 版本29

任何幫助將不勝感激。

基本上點擊按鈕,你就開始服務。 而不是這個

startActivity(displayTost);

startService(new Intent(MainActivity.this, MyBgService.class));

MyBgService是一個服務 它不是一個活動。 你不能這樣做

startActivity(displayTost);

而不是做

startService(displayTost);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM