我对应用开发很新。我正在关注developer.android.com上的教程,并正在制作网站上给出的“我的第一个应用程序”应用程序。我正在使用eclipse,每当我在手机上启动应用程序时,我都会收到错误消息,说不幸的是我的第一个应用程序已停止工作。我该怎么办?
代码:
package com.example.myfirstapp;
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 MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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);
}
/**
* 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_main, container, false);
return rootView;
}
}
}
LogCat:
03-18 12:27:42.529: W/dalvikvm(6930): VFY: unable to resolve static field 1559 (ActionBarWindow) in Landroid/support/v7/appcompat/R$styleable;
03-18 12:27:42.529: D/dalvikvm(6930): VFY: replacing opcode 0x62 at 0x0004
03-18 12:27:42.534: D/AndroidRuntime(6930): Shutting down VM
03-18 12:27:42.534: W/dalvikvm(6930): threadid=1: thread exiting with uncaught exception (group=0x4185d2a0)
03-18 12:27:42.539: E/AndroidRuntime(6930): FATAL EXCEPTION: main
03-18 12:27:42.539: E/AndroidRuntime(6930): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:107)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
03-18 12:27:42.539: E/AndroidRuntime(6930): at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:18)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.Activity.performCreate(Activity.java:5188)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.ActivityThread.access$700(ActivityThread.java:140)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.os.Looper.loop(Looper.java:137)
03-18 12:27:42.539: E/AndroidRuntime(6930): at android.app.ActivityThread.main(ActivityThread.java:4921)
03-18 12:27:42.539: E/AndroidRuntime(6930): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 12:27:42.539: E/AndroidRuntime(6930): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 12:27:42.539: E/AndroidRuntime(6930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-18 12:27:42.539: E/AndroidRuntime(6930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-18 12:27:42.539: E/AndroidRuntime(6930): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
将apptheme更改为manifest.xml中的Theme.AppCompat
<application
...
android:theme="@style/Theme.AppCompat" >
答案 1 :(得分:0)
将以下内容添加到清单文件
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="18" />