我是android编程的新手
我正在尝试执行我从here撰写的应用,
但我无法运行该应用。当我尝试运行应用程序时,模拟器会显示
Abc(app name)突然停止工作
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
MainActivity.java
package com.xyz.abc;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
private static final String EXTRA_MESSAGE = "com.xyz.Abc.MESSAGE";
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
}
DisplayMessageActivity.java
package com.xyz.abc;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
fragment_display_message.xml
<LinearLayout 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.xyz.abc.DisplayMessageActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/edit_message" />
</LinearLayout>
下面的是错误的logcat输出。
05-07 06:11:21.004:D / AndroidRuntime(1143):关闭虚拟机 05-07 06:11:21:2004:W / dalvikvm(1143):threadid = 1:线程退出未捕获异常(group = 0xb2aabba8) 05-07 06:11:21.134:E / AndroidRuntime(1143):致命异常:主要 05-07 06:11:21.134:E / AndroidRuntime(1143):进程:com.xyz.abc,PID:1143 05-07 06:11:21.134:E / AndroidRuntime(1143):java.lang.RuntimeException:无法启动活动 ComponentInfo {} com.xyz.abc/com.xyz.abc.MainActivity: java.lang.IllegalStateException:您需要使用Theme.AppCompat 主题(或后代)与此活动。 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.os.Handler.dispatchMessage(Handler.java:102) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.os.Looper.loop(Looper.java:136) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread.main(ActivityThread.java:5017) 05-07 06:11:21.134:E / AndroidRuntime(1143):at java.lang.reflect.Method.invokeNative(Native Method) 05-07 06:11:21.134:E / AndroidRuntime(1143):at java.lang.reflect.Method.invoke(Method.java:515) 05-07 06:11:21.134:E / AndroidRuntime(1143):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 05-07 06:11:21.134:E / AndroidRuntime(1143):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-07 06:11:21.134:E / AndroidRuntime(1143):at dalvik.system.NativeStart.main(Native Method) 05-07 06:11:21.134:E / AndroidRuntime(1143):引起:java.lang.IllegalStateException:你需要使用Theme.AppCompat 主题(或后代)与此活动。 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 05-07 06:11:21.134:E / AndroidRuntime(1143):at com.xyz.abc.MainActivity.onCreate(MainActivity.java:29) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.Activity.performCreate(Activity.java:5231) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 05-07 06:11:21.134:E / AndroidRuntime(1143):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 05-07 06:11:21.134:E / AndroidRuntime(1143):......还有11个 05-07 06:11:29.494:I / Process(1143):发送信号。 PID:1143 SIG:9
答案 0 :(得分:0)
You need to use a Theme.AppCompat theme (or descendant) with this activity.
这就是您的应用崩溃的原因。查看您的清单,找到您的活动或应用程序的主题,并确保它扩展了一些AppCompat主题,例如。最新Android Studio中的自动生成项目如下:
的的AndroidManifest.xml 强>
<application
...
android:theme="@style/AppTheme">
<强> styles.xml 强>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
...
</style>