应用程序在使用操作栏时崩溃,出了什么问题?

时间:2013-09-02 17:34:01

标签: android crash action

所以我正在学习制作Android应用程序的基础知识,现在我正在尝试动作栏。它不会给出任何错误,但是当我在手机上运行应用程序时它会崩溃。 所以这是动作栏:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_settings"
      android:title="@string/action_settings"
      android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_action_search"
      android:showAsAction="always" />          

和主要活动java:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
        startActivity(new Intent(this, Hallo.class));
            return true;
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }

这是hallo.class

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="@string/hallo" />

和hallo.java:

package com.example.myfirstapp;

public class Hallo extends MainActivity{
protected void onCreate() {
    setContentView(R.layout.hallo);
}
}

所以这就是我所拥有的。怎么了? 谢谢你的回复。 现在我还在这里,“这个”是什么意思? 再次感谢。

编辑 我认为这是堆栈跟踪

Thread [<1> main] (Suspended (exception ActivityNotFoundException)) 
    <VM does not provide monitor information>   
    Instrumentation.checkStartActivityResult(int, Object) line: 1556    
    Instrumentation.execStartActivity(Context, IBinder, IBinder, Activity, Intent, int, Bundle) line: 1431  
    MainActivity(Activity).startActivityForResult(Intent, int, Bundle) line: 3390   
    MainActivity(Activity).startActivityForResult(Intent, int) line: 3351   
    MainActivity(Activity).startActivity(Intent, Bundle) line: 3561 
    MainActivity(Activity).startActivity(Intent) line: 3529 
    MainActivity.onOptionsItemSelected(MenuItem) line: 35   
    MainActivity(Activity).onMenuItemSelected(int, MenuItem) line: 2573 
    PhoneWindow.onMenuItemSelected(MenuBuilder, MenuItem) line: 1059    
    MenuBuilder.dispatchMenuItemSelected(MenuBuilder, MenuItem) line: 735   
    MenuItemImpl.invoke() line: 149 
    MenuBuilder.performItemAction(MenuItem, int) line: 874  
    ActionMenuView.invokeItem(MenuItemImpl) line: 603   
    ActionMenuItemView.onClick(View) line: 148  
    ActionMenuItemView(View).performClick() line: 4162  
    View$PerformClick.run() line: 17082 
    Handler.handleCallback(Message) line: 615   
    ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 4856    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 511  
    ZygoteInit$MethodAndArgsCaller.run() line: 1007 
    ZygoteInit.main(String[]) line: 774 
    NativeStart.main(String[]) line: not available [native method]  

3 个答案:

答案 0 :(得分:0)

我猜你没有在清单文件中添加Mainactivity或Hallo。请发布你的androidManifest.xml

答案 1 :(得分:0)

您需要在应用的AndroidManifest.xml中添加MainActivity和Hallo活动。

PS。 MainActivity不是抽象类。

答案 2 :(得分:0)

在应用程序标记中的清单xml中:

<activity class="Hallo" android:label="@string/app_name" android:name=".Hallo" />