ActivityNotFoundException:无法找到显式活动类?

时间:2016-12-30 19:56:10

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

是的,我知道这个问题已被问过很多次了,但这些答案对我来说都没有用吗?我想也许有人只需要解释我的代码。

我的应用程序在SplashScreen.java上启动10秒钟,然后应该加载MainActivity.java。但是,当我运行我的应用程序时,它会启动SplashScreen,但是在10秒后,MainActivity不加载,应用程序说它已停止。

我检查了运行日志,它说“android.content.ActivityNotFoundException:无法找到显式活动类{com.msp.supercarsounds / com.msp.supercarsounds.MainActivity};您是否已在AndroidManifest.xml中声明此活动?“

的AndroidManifest.xml:

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

    <uses-sdk android:minSdkVersion="17"
              android:targetSdkVersion="22"/>
    
    <uses-permission
        android:name="android.permission.INTERNET"/>

    <application
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@mipmap/carsounds_logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.msp.supercarsounds.SplashScreen"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
        <activity
            android:name="com.msp.supercarsounds.MainActivity" />

        <activity
            android:name=".AudiPrimary"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.RegisterApplication"
            android:label="Register Appliction"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.msp.supercarsounds.Porsche"
            android:label="Porsche"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.MercedesBenz"
            android:label="Mercedes-Benz"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.Lamborghini"
            android:label="Lamborghini"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.msp.supercarsounds.Ferrari"
            android:label="Ferrari"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.BMW"
            android:label="BMW"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.Jaguar"
            android:label="Jaguar"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.AstonMartin"
            android:label="Aston Martin"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.msp.supercarsounds.ChooseManufacturer"
            android:label="Choose Manufacturer"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.Audi"
            android:label="Audi"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar" />

        <activity
            android:name="com.msp.supercarsounds.About"
            android:label="About"
            android:theme="@style/AppTheme" />

        <activity
            android:name="com.msp.supercarsounds.Contact"
            android:label="Contact"
            android:theme="@style/AppTheme" />

        <service
            android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action
                    android:name="com.google.firebase.MESSAGING.EVENT"/>
            </intent-filter>
        </service>

        <service
            android:name=".MyFirebaseInstanceIDService">
            <intent-filter>
                <action
                    android:name="com.google.firebase.INSTANCE.ID.EVENT"/>
            </intent-filter>
        </service>

</manifest>


SplashScreen.java:

package com.msp.supercarsounds;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashScreen extends AppCompatActivity {

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

    Thread myThread = new Thread() {

        @Override
        public void run() {
            try {
                sleep(10000);
                Intent startMainActivity = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(startMainActivity);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    };
    myThread.start();
}


}

MainActivity.java:

package com.msp.supercarsounds;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;


public class MainActivity extends AppCompatActivity {

    public static final String PREFS_NAME = "MyPrefsFile1";
    public CheckBox dontShowAgain;

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

    }


    public void clickedAbout(View view) {

        final int result = 1;

        Intent AboutButtonClicked = new Intent (this, About.class);
        AboutButtonClicked.putExtra("About", "MainActivity");
        startActivityForResult(AboutButtonClicked, result);

    }

    public void clickedContact(View view) {

        final int result = 1;

        Intent ContactButtonClicked = new Intent (this, Contact.class);
        ContactButtonClicked.putExtra("Contact", "MainActivity");
        startActivityForResult(ContactButtonClicked, result);

    }
    @Override
    protected void onResume() {
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        LayoutInflater adbInflater = LayoutInflater.from(this);
        View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String skipMessage = settings.getString("skipMessage", "NOT checked");

        dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
        adb.setView(eulaLayout);
        adb.setTitle("PLEASE REGISTER THIS APPLICATION!");
        adb.setMessage(Html.fromHtml("Please register this application in order to receive newer versions and support!"));

        adb.setPositiveButton("I understand", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";

                if (dontShowAgain.isChecked()) {
                    checkBoxResult = "checked";
                }

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString("skipMessage", checkBoxResult);
                editor.commit();

                // Do what you want to do on "OK" action

                return;
            }
        });

        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String checkBoxResult = "NOT checked";

                if (dontShowAgain.isChecked()) {
                    checkBoxResult = "checked";
                }

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();

                editor.putString("skipMessage", checkBoxResult);
                editor.commit();


                return;
            }
        });

        if (!skipMessage.equals("checked")) {
            adb.show();
        }

        super.onResume();
    }

    public void clickedSettings(MenuItem item) {

        final int result = 1;

        Intent SettingsButtonClicked = new Intent (this, About.class);
        SettingsButtonClicked.putExtra("About", "MainActivity");
        startActivityForResult(SettingsButtonClicked, result);

    }

    public void clickedSupercarSounds(View view) {

        final int result = 1;

        Intent SupercarSoundsButtonClicked = new Intent (this, ChooseManufacturer.class);
        SupercarSoundsButtonClicked.putExtra("Choose Manufacturer", "MainActivity");
        startActivityForResult(SupercarSoundsButtonClicked, result);

    }

    public void clickedRegisterApplication(View view) {

        final int result = 1;

        Intent RegisterApplicationButtonClicked = new Intent (this, RegisterApplication.class);
        RegisterApplicationButtonClicked.putExtra("Register Application", "MainActivity");
        startActivityForResult(RegisterApplicationButtonClicked, result);

    }

}

谢谢!

1 个答案:

答案 0 :(得分:4)

您需要将<activity>标记放在AndroidManifest.xml中的<application>标记中。