我正在关注如何创建Android应用程序的android.developer教程,并遇到了一个我不知道如何解决的错误。我在课程中遇到了关于在添加menuinflater代码时向菜单栏(http://developer.android.com/training/basics/actionbar/adding-buttons.html)
添加操作按钮的错误。
当我尝试运行应用程序时,它崩溃了一条消息"不幸的是,我的第一个应用程序停止了#34;我在Eclipse的logcat输出窗口中得到以下内容:
07-01 05:23:20.755: I/Choreographer(1181): Skipped 42 frames! The application may be doing too much work on its main thread.
07-01 05:23:20.775: W/ResourceType(1181): getEntry failing because entryIndex 2 is beyond type entryCount 2
07-01 05:23:20.775: W/ResourceType(1181): Failure getting entry for 0x7f0c0002 (t=11 e=2) in package 0 (error -2147483647)
07-01 05:23:20.785: D/AndroidRuntime(1181): Shutting down VM
07-01 05:23:20.785: W/dalvikvm(1181): threadid=1: thread exiting with uncaught exception (group=0xb2a39ba8)
07-01 05:23:20.795: E/AndroidRuntime(1181): FATAL EXCEPTION: main
07-01 05:23:20.795: E/AndroidRuntime(1181): Process: com.example.myfirstapp, PID: 1181
07-01 05:23:20.795: E/AndroidRuntime(1181): android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0002
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.content.res.Resources.getValue(Resources.java:1123)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.content.res.Resources.getLayout(Resources.java:939)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:115)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.example.myfirstapp.MainActivity.onCreateOptionsMenu(MainActivity.java:35)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.view.Choreographer.doFrame(Choreographer.java:543)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.os.Handler.handleCallback(Handler.java:733)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.os.Handler.dispatchMessage(Handler.java:95)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.os.Looper.loop(Looper.java:136)
07-01 05:23:20.795: E/AndroidRuntime(1181): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 05:23:20.795: E/AndroidRuntime(1181): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 05:23:20.795: E/AndroidRuntime(1181): at java.lang.reflect.Method.invoke(Method.java:515)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 05:23:20.795: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 05:23:20.795: E/AndroidRuntime(1181): at dalvik.system.NativeStart.main(Native Method)
DisplayMessageActivity.java:
package com.example.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@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;
}
}
}
main_activity_actions.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
有人知道如何解决这个问题吗?
如果有人知道使用android.developer教程学习Android开发的更好方法,请分享一下,尝试这些教程时遇到的错误实际上非常荒谬。
答案 0 :(得分:0)
我认为您没有添加ic_action_search drawable或字符串资源。崩溃表明您的项目中缺少与菜单xml相关的资源,或者您将它们放在错误的资源文件夹中。