java.lang.RuntimeException : Unable to instantiate activit

标签 java android manifest runtimeexception

我正在尝试编写一个 Android 应用程序,当您单击按钮时,它会将文本输入控制台,但是当我编译它并在运行 7.0 API 版本 24 的 Galaxy S7 Edge 上运行它时,我收到错误.

I've read that I need to add something into the manifest file但我不确定我需要添加什么。

FATAL EXCEPTION: main Process: me.adamstephenson.test.test1, PID: 16405 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{me.adamstephenson.test.test1/me.adamstephenson.test.test1.MainActivity}: java.lang.ClassCastException: me.adamstephenson.test.test1.MainActivity cannot be cast to android.app.Activity

package me.adamstephenson.test.test1;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity {
    public class MyActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.activity_main);

            final Button button = (Button) findViewById(R.id.RequestKey);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.d("RequestKey", "Clicked");
                }
            });
        }
    }
}

Source

这是布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.adamstephenson.test.test1.MainActivity">

<Button
    android:id="@+id/RequestKey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:layout_editor_absoluteX="196dp"
    tools:layout_editor_absoluteY="129dp" />
</android.support.constraint.ConstraintLayout>

最后是 list 文件

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

最佳答案

您在 list 中引用了错误的类。 MyActivity 是 Activity,而不是 MainActivity。将您的 list 更改为以下内容:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity$MyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

还有

你也应该让你的内心 Activity 静态:

public class MainActivity {
    public static class MyActivity extends Activity {
        ...
    }
}

关于java.lang.RuntimeException : Unable to instantiate activit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42728010/

相关文章:

android - FAB - Lollipop 前的正方形, Lollipop 上没有阴影

android - 改造:失败状态代码返回空指针。

scala - 使用 list 实例化 View 有界类型

java - Maven 阴影插件无法解析 javax.jnlp

java - 使用正则表达式删除电子邮件地址后的尾随字符

java - Android 相当于 iOS 的 beginIgnoringInteractionEvents

android - 如何知道 RecyclerView 中的最后一项

java - 如何在 Android 10 (Android Q) 中访问外部存储?

来自 gradle.properties 的 Android manifestPlaceholders

java - 使用 Postfix 通过 Java 发送电子邮件