我是初学者,我正在使用树屋学习android开发。在我做的两个应用程序中,两个都从今天开始崩溃。我不确定为什么会这样,因为我一直在遵循他们的所有指示。
主:
package com.example.personalityidentifier;
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 Personality mPersonality = new Personality();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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 = mPersonality.getAPersonality();
// 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);
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.personalityidentifier"
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.personalityidentifier.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-10 10:47:22.870: D/AndroidRuntime(886): Shutting down VM
07-10 10:47:22.870: W/dalvikvm(886): threadid=1: thread exiting with uncaught exception (group=0xb1a78ba8)
07-10 10:47:22.940: E/AndroidRuntime(886): FATAL EXCEPTION: main
07-10 10:47:22.940: E/AndroidRuntime(886): Process: com.example.personalityidentifier, PID: 886
07-10 10:47:22.940: E/AndroidRuntime(886): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.personalityidentifier/com.example.personalityidentifier.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.os.Looper.loop(Looper.java:136)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-10 10:47:22.940: E/AndroidRuntime(886): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 10:47:22.940: E/AndroidRuntime(886): at java.lang.reflect.Method.invoke(Method.java:515)
07-10 10:47:22.940: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-10 10:47:22.940: E/AndroidRuntime(886): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-10 10:47:22.940: E/AndroidRuntime(886): at dalvik.system.NativeStart.main(Native Method)
07-10 10:47:22.940: E/AndroidRuntime(886): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
07-10 10:47:22.940: E/AndroidRuntime(886): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
07-10 10:47:22.940: E/AndroidRuntime(886): at com.example.personalityidentifier.MainActivity.onCreate(MainActivity.java:16)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.Activity.performCreate(Activity.java:5231)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-10 10:47:22.940: E/AndroidRuntime(886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-10 10:47:22.940: E/AndroidRuntime(886): ... 11 more
07-10 10:47:26.170: I/Process(886): Sending signal. PID: 886 SIG: 9
答案 0 :(得分:0)
在黑暗中拍摄,在TextView中删除Final。看看是否有效。
答案 1 :(得分:0)
错误日志会更好地了解问题,
试试这个。
第1步:创建BlankActivity。
第2步:MainActivity.class
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button)findViewById(R.id.button1);
text =(TextView)findViewById(R.id.text);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setText("Hello Android!");
}
});
}
@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);
}
}
第3步:布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hrh.MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Click me" />
</RelativeLayout>
第4步:****清单文件,注意*主题必须是AppCompat看下面的样式代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hrh"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
**步骤5:**值dir中的Style.xml。
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>