我是初学者,一直在使用树屋学习。我很早就在课程中,当我启动应用程序时突然开始收到崩溃消息。我不确定发生了什么。我发布了我所知道的一切。对不起,如果我错过了什么或张贴太多。
主:
package com.example.crystalball;
import android.R.string;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private CrystalBall mCrystalBall = new CrystalBall();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View variables
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String answer = mCrystalBall.getAnAnswer();
// Update label with dynamic answer
answerLabel.setText(answer);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
}
CrystalBall:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crystalball"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.crystalball.MainActivity"
android:label="@string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.crystalball"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<activity
android:name="com.example.crystalball.MainActivity"
android:label="@string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的:
07-28 01:50:31.530: D/AndroidRuntime(980): Shutting down VM
07-28 01:50:31.530: W/dalvikvm(980): threadid=1: thread exiting with uncaught exception (group=0xb1ae7ba8)
07-28 01:50:31.570: E/AndroidRuntime(980): FATAL EXCEPTION: main
07-28 01:50:31.570: E/AndroidRuntime(980): Process: com.example.crystalball, PID: 980
07-28 01:50:31.570: E/AndroidRuntime(980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.os.Handler.dispatchMessage(Handler.java:102)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.os.Looper.loop(Looper.java:136)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-28 01:50:31.570: E/AndroidRuntime(980): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 01:50:31.570: E/AndroidRuntime(980): at java.lang.reflect.Method.invoke(Method.java:515)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-28 01:50:31.570: E/AndroidRuntime(980): at dalvik.system.NativeStart.main(Native Method)
07-28 01:50:31.570: E/AndroidRuntime(980): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
07-28 01:50:31.570: E/AndroidRuntime(980): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:16)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.Activity.performCreate(Activity.java:5231)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-28 01:50:31.570: E/AndroidRuntime(980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-28 01:50:31.570: E/AndroidRuntime(980): ... 11 more
07-28 01:50:37.070: I/Process(980): Sending signal. PID: 980 SIG: 9
答案 0 :(得分:4)
您的活动延伸ActionBarActivity
。
public class MainActivity extends ActionBarActivity
表示您正在使用AppCompat
,在这种情况下,您需要将Theme
从Theme.AppCompat
设置为活动
或
android:theme="@style/Theme.AppCompat" >
设置应用程序的主题
答案 1 :(得分:0)
要简单地添加ActionBar Compat,您的活动或应用应在@style/Theme.AppCompat
中使用AndroidManifest.xml
主题,如下所示:
<activity
...
android:theme="@style/Theme.AppCompat" />
这将在activty中添加actionbar(如果您将此主题添加到应用程序,则添加所有活动)
即。在您的清单文件中,更改
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
到
android:theme="@style/Theme.AppCompat"
答案 2 :(得分:0)
请更改
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
到
android:theme="@android:style/Theme.AppCompat"