[英]How to fix 'IllegalStateException' (fatal exception) in android studio?
[英]How to fix an android 'IllegalStateException' error?
我對 android 很陌生,剛剛開始了解如何開發簡單的應用程序( AndroidStudio
、 Ubuntu 14.04
、 LG G3)。 我想從一個主要活動開始另一個活動(即顯示一個不同的屏幕,用戶可以在其中進行一些輸入)遵循此解決方案。 在文件MainActivity.java
我有以下方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Intent myIntent = new Intent(MainActivity.this, NewEntryActivity.class);
MainActivity.this.startActivity(myIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
文件NewEntryActivity.java
定義如下:
package com.example.alexander.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
public class NewEntryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_newentry);
final EditText editNewIsin = (EditText) findViewById(R.id.new_isin);
final EditText editNewPrice = (EditText) findViewById(R.id.new_price);
final EditText editNewNumber = (EditText) findViewById(R.id.new_number);
Button buttonNewOk = (Button) findViewById(R.id.new_ok);
Button buttonNewCancel = (Button) findViewById(R.id.new_cancel);
}
}
此文件未指示任何錯誤(似乎所有內容都已正確定義)。 當我在手機上啟動應用程序時,主要活動開始沒有問題,但是當我選擇菜單項啟動其他活動時,應用程序立即關閉,我看到一個錯誤:
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
但是,這兩個活動都是從AppCompatActivity
派生的。 也許它指的是其他東西(Manifest,layout.xml,...?),但這從錯誤消息中並不清楚。 任何幫助在這里表示贊賞......
這里也是清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alexander.myapplication" >
<application>
android:allowBackup="true"
android:icon="@mipmap/stoxx"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewEntryActivity"
android:label="@string/menu_add"
/>
</application>
</manifest>
只需在其他活動中使用與 home 相同的主題即可。 像這樣
<activity
android:name=".SecondActivity"
android:label="@string/second"
android:theme="@style/AppTheme.NoActionBar" >
</activity>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.