gpt4 book ai didi

java - 创建菜单资源会抛出 IllegalStateException

转载 作者:搜寻专家 更新时间:2023-11-01 09:28:30 24 4
gpt4 key购买 nike

我有两个 Activity 。 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>

最佳答案

尝试下面的完整示例:

主要 Activity :----------------

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;
}}

登录 Activity :----------------

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;
}

}

飞溅 Activity :----------------

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();
}
}

布局:----------------

主要布局:

<?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>

登录布局:

<?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>

菜单:

主菜单:

<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>

登录菜单:

<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>

list :

<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>

主题:--------------------

应用主题:

<!-- 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>

飞溅主题:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
</style>

注意:使用“super”作为登录密码。

关于java - 创建菜单资源会抛出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49190369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com