在使用android:theme时爆出了一个java.lang.IllegalStateException

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity异常的处理:


异常出现的过程:

使用android:theme="@android:style/Theme.DeviceDefault.Dialog"来改变程序的主题

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jxust.elts"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jxust.elts.activity.LoginActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.DeviceDefault.Dialog" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

一运行就报了以下的异常:

<span style="font-size:14px;">07-04 07:43:07.200: I/Choreographer(2169): Skipped 37 frames!  The application may be doing too much work on its main thread.
07-04 07:49:05.660: E/AndroidRuntime(2316): Process: com.jxust.elts, PID: 2316
07-04 07:49:05.660: E/AndroidRuntime(2316): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jxust.elts/com.jxust.elts.activity.LoginActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-04 07:49:05.660: E/AndroidRuntime(2316): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
07-04 07:49:05.660: E/AndroidRuntime(2316): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)</span>

异常出现的原因:

package com.jxust.elts.activity;

import android.support.v7.app.ActionBarActivity;

import com.jxust.elts.R;
import com.jxust.elts.R.id;
import com.jxust.elts.R.layout;
import com.jxust.elts.R.menu;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class LoginActivity extends ActionBarActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is
		// present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

在LoginActivity.java中继承了ActionBarActivity就导致了这个异常的发生


解决办法:

将继承ActionBarActivity改为继承Activity即可解决这个问题


最后的效果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值