#android #eclipse #android-actionbar
#Android #eclipse #android-панель действий
Вопрос:
Я начинаю изучать, как создавать приложения для Android. Я пытался добавить кнопки на панель действий, когда приложение начало сбоить. Я предоставлю MainActivity.java содержимое, а также содержимое logcat.
logcat:
07-04 13:31:15.901: E/AndroidRuntime(14357): FATAL EXCEPTION: main
07-04 13:31:15.901: E/AndroidRuntime(14357): android.content.res.Resources$NotFoundException: Resource ID #0x0
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.content.res.Resources.getValue(Resources.java:1981)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.content.res.Resources.loadXmlResourceParser(Resources.java:3187)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.content.res.Resources.getLayout(Resources.java:1797)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:115)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.example.test.MainActivity.onCreateOptionsMenu(MainActivity.java:32)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:147)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:285)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.view.Choreographer.doFrame(Choreographer.java:560)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.os.Handler.handleCallback(Handler.java:730)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.os.Looper.loop(Looper.java:137)
07-04 13:31:15.901: E/AndroidRuntime(14357): at android.app.ActivityThread.main(ActivityThread.java:5455)
07-04 13:31:15.901: E/AndroidRuntime(14357): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 13:31:15.901: E/AndroidRuntime(14357): at java.lang.reflect.Method.invoke(Method.java:525)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-04 13:31:15.901: E/AndroidRuntime(14357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
MainActivity.java:
public class MainActivity extends ActionBarActivity {
static final String EXTRA_MESSAGE = "com.example.test.MESSAGE";
@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 items for use in the action bar
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);
}
public void sendMessage(View view) {
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);
}
/**
* 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;
}
}
}
Комментарии:
1. action_settings созданы в res/menu/main_activity_actions.xml ?
2. Где определено getMenuInflater()?
3. Возможно ли, что приложение не может найти R.menu.main_activity_actions?
4. @ChitownDev Это определено в Activity
Ответ №1:
Попробуйте заменить вашу функцию меню на:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
Я думаю, что вызов super не был необходим и сбивал с толку.