我有两项活动。仅显示启动画面的 MainActivity.java 。显示登录屏幕的 LoginActivity.java 。我已将样式定义为 Theme.AppCompat.Light.NoActionBar 。我想在LoginActivity上添加一个操作栏和菜单。当我创建菜单资源时,即使它尚未初始化,应用程序也会崩溃并出现以下错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sidyeti.userapp, PID: 12539
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sidyeti.userapp/com.example.sidyeti.userapp.activities.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.sidyeti.userapp.activities.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的MainActivity.java
package com.example.sidyeti.userapp.activities;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import com.example.sidyeti.userapp.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showSplashScreen();
}
private void showSplashScreen() {
int SPLASH_TIME_OUT = 2500;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i =new Intent(MainActivity.this,LoginActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
这是我创建的菜单资源(但尚未使用/初始化)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Settings" />
</menu>
这是我的style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
为什么即使在菜单初始化之前应用程序也会崩溃?如果我删除菜单资源,该应用程序工作正常。 请注意我不想要MainActivity中的菜单。
修改 这是我的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sidyeti.userapp">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.LoginActivity" />
<activity android:name=".activities.SignupActivity" />
<activity android:name=".activities.UserActivity" />
<activity android:name=".activities.ScannerActivity" />
</application>
</manifest>
答案 0 :(得分:1)
请尝试以下完整示例:
主要活动:----------------
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
//menu handling
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.m_settings:
Toast.makeText(getApplicationContext(),"Main Menu",Toast.LENGTH_LONG).show();
break;
default:
throw new RuntimeException("unknown menu selection");
}
return true;
}}
登录活动:--------------
public class LoginActivity extends AppCompatActivity {
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
password = (EditText) findViewById(R.id.edt);
password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.toString().equals("super")){
finish();
Intent information_intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(information_intent);
}
}
});
}
//menu handling
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.m1_settings:
Toast.makeText(getApplicationContext(),"Login Menu",Toast.LENGTH_LONG).show();
break;
default:
throw new RuntimeException("unknown menu selection");
}
return true;
}
}
启动活动:--------------
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
Thread timer = new Thread() {
@Override
public void run() {
super.run();
try {
sleep(2500); // this time is required for the proper reception of the message by the android and the ability of the arduino to transmit/recieve a new message
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
Intent information_intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(information_intent);
}
}
};
timer.start();
}
}
布局:-----------------
main_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Activity"/>
</LinearLayout>
login_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter Password..."
android:id="@+id/edt"
android:text="xxxxx"/>
</LinearLayout>
splash_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Splash Activity"/>
</LinearLayout>
菜单的:
MAIN_MENU:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:enabled="true"
android:titleCondensed="settings"
android:title="settings" android:id="@+id/m_settings"
android:orderInCategory="1" />
</menu>
login_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:enabled="true"
android:titleCondensed="settings"
android:title="settings" android:id="@+id/m1_settings"
android:orderInCategory="1" />
</menu>
清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="com.example.admin.machinelearning.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="com.example.admin.machinelearning.LoginActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
主题:------------------
AppTheme:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
SplashTheme:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
</style>
注意:使用“super”作为密码登录。
答案 1 :(得分:1)
我没有解决方案,由于评论中的空格而在此发帖:
问题似乎出现在AppCompatDelegateImplV9.java
中TypedArray a = mContext.obtainStyledAttributes(R.styleable.AppCompatTheme);
if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
a.recycle();
throw new IllegalStateException(
"You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}
在许多页面中,这个问题被指向一个错误,我没有找到任何实际解决方案的错误跟踪。
在另一个问题中,通过清除Android Studio崩溃解决了类似的问题。你可以尝试一下,看看它是否有效: