我刚刚开始学习Android / Java,目前正在阅读一本书。我正在进行的活动非常简单,它解释了活动的生命周期,然后解释了如何使用“材质主题”,并说明了如何更改代码来使用它。当我更改代码时(主要添加此"android:theme="@android:style/Theme.Material">"
并运行它,我得到:
"You need to use a Theme.AppCompat theme (or descendant) with this activity."
在AVD屏幕上,我也收到“ Activity101保持停止”的信息。在开始时,我不知道为什么要这样或如何解决它。 我正在使用Android Studio 3.2.1和Nexus 5X API 25 AVD。任何帮助都会很棒。
活动
package com.markusmartius.activity101;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity
{
String tag = "Lifecycle Step";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag, "In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag, "In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag, "In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag, "In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag, "In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag, "In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag, "In the onDestroy() event");
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.markusmartius.activity101">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@android:style/Theme.Material">
<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>
错误日志(如果需要更多信息,请告诉我)
W/System: ClassLoader referenced unknown path: /data/app /com.markusmartius.activity101-1/lib/x86
I/InstantRun: starting instant run server: is main process
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(andro id.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.markusmartius.activity101, PID: 19455
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.markusmartius.activity101/com.markusmartius.activity101.Mai nActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)