我正在尝试编写一个Android应用程序,当您单击按钮时会将文本放入控制台,但是当我编译并在运行7.0 API版本24的Galaxy S7边缘上运行它时出现错误
I've read that I need to add something into the manifest file但是我不确定我需要添加什么。
致命的例外:主要 过程:me.adamstephenson.test.test1,PID:16405 java.lang.RuntimeException:无法实例化活动ComponentInfo {me.adamstephenson.test.test1 / me.adamstephenson.test.test1.MainActivity}:java.lang.ClassCastException:me.adamstephenson.test.test1.MainActivity无法强制转换为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");
}
});
}
}
}
这是布局
<?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>
最后这里是清单文件
<?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>
答案 0 :(得分:1)
您指的是清单中的错误类。 MyActivity
是活动,而不是MainActivity
。将您的清单更改为以下内容:
<?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>
和强>
你也应该进行内心活动static
:
public class MainActivity {
public static class MyActivity extends Activity {
...
}
}
答案 1 :(得分:1)
因为您的活动类是作为内部类和静态类创建的。我不知道你为什么选择这种方式,而你可以这样正确地使用它:
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");
}
});
}
.
.
.
// other methods or inner class can exist here
}