我正在关注developer.android.com培训,我刚刚学会了如何添加文本和按钮,我不认为我已经改变了任何东西并完成了同样的事情。我没有错误,但是当我在手机上部署时,它会崩溃说
遗憾的是MyFirstApp已停止工作
。请检查我的代码,看看是否有任何错误。我也尝试过清洁和重建,它仍然会崩溃。
这是我的MainActivity.java
package com.MyFirstApp.yoo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@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;
}
@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);
}
}
这是我的activity_main.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:orientation="horizontal"
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.MyFirstApp.yoo.MainActivity" >
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_message" >
</EditText>
<button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>
</LinearLayout>
这是我的strings.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings </string>
<string name="title_activity_main">MainActivity</string>
</resources>
这是我的MyFirstApp Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyFirstApp.yoo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:targetSdkVersion="21" android:minSdkVersion="8"/>
<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>
logcat
07-24 19:05:54.175: W/dalvikvm(32427): VFY: unable to resolve static field 1864 (ActionBarWindow) in Landroid/support/v7/appcompat/R$styleable;
07-24 19:05:54.175: D/dalvikvm(32427): VFY: replacing opcode 0x62 at 0x0004
07-24 19:05:54.175: D/AndroidRuntime(32427): Shutting down VM
07-24 19:05:54.175: W/dalvikvm(32427): threadid=1: thread exiting with uncaught exception (group=0x416502a0)
07-24 19:05:54.235: E/AndroidRuntime(32427): FATAL EXCEPTION: main
07-24 19:05:54.235: E/AndroidRuntime(32427): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:106)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99)
07-24 19:05:54.235: E/AndroidRuntime(32427): at com.MyFirstApp.yoo.MainActivity.onCreate(MainActivity.java:12)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.Activity.performCreate(Activity.java:5206)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.os.Looper.loop(Looper.java:137)
07-24 19:05:54.235: E/AndroidRuntime(32427): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 19:05:54.235: E/AndroidRuntime(32427): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 19:05:54.235: E/AndroidRuntime(32427): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 19:05:54.235: E/AndroidRuntime(32427): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-24 19:05:54.235: E/AndroidRuntime(32427): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-24 19:05:54.235: E/AndroidRuntime(32427): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
请提供错误日志。
也尝试使用,
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>
而不是
<button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"/>