Java 很遗憾,您的应用程序已停止

Java 很遗憾,您的应用程序已停止,java,android,Java,Android,我一直收到这条消息,不幸的是你的应用程序在不知道原因的情况下停止了 这是我的主要任务 import android.app.ActionBar; import android.app.Activity; import android.app.SearchManager; import android.content.Context; import android.os.Bundle; import android.support.v7.widget.SearchView; import andr

我一直收到这条消息,不幸的是你的应用程序在不知道原因的情况下停止了

这是我的主要任务

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {
private ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    actionBar = getActionBar();
    // add the custom view to the action bar
    actionBar.setCustomView(R.layout.actionbar_view);
    EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
    search.setOnEditorActionListener(new OnEditorActionListener() {

      @Override
    public boolean onEditorAction(TextView v, int actionId,
          KeyEvent event) {

        return true;
      }
    });
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
        | ActionBar.DISPLAY_SHOW_HOME);

    }
    catch (NullPointerException ex)
    {
        throw new IllegalStateException("null property", ex);
    }
}

@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);





    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search_action)
            .getActionView();
    //searchView.setSearchableInfo(searchManager
    //      .getSearchableInfo(getComponentName()));
    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;
    }
    if (id == R.id.search_action)
        return true;
    return super.onOptionsItemSelected(item);

}
}
问题的根源是主要的 我没有一个代码错误 而且这从来都不是支架的问题 我还尝试了从清单中更改主题的解决方案

这是我的舱单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.folowreader"
android:versionCode="1"
android:versionName="1.0" >

 <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

 <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>
这是我的R课

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

 package com.example.folowreader;

 public final class R {
 public static final class attr {
 }
 public static final class color {
    public static final int light_blue=0x7f040000;
 }
 public static final class dimen {
    /**  Default screen margins, per the Android Design guidelines. 

     Example customization of dimensions originally defined in res/values/dimens.xml
     (such as screen margins) for screens with more than 820dp of available width. This
     would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).

     */
    public static final int activity_horizontal_margin=0x7f050000;
    public static final int activity_vertical_margin=0x7f050001;
  }
  public static final class drawable {
    public static final int ic_action_search=0x7f020000;
    public static final int ic_launcher=0x7f020001;
  }
  public static final class id {
    public static final int action_settings=0x7f090005;
    public static final int button1=0x7f090002;
    public static final int button2=0x7f090003;
    public static final int ic_action_search=0x7f090007;
    public static final int search_action=0x7f090006;
    public static final int searchfield=0x7f090000;
    public static final int tableRow1=0x7f090001;
    public static final int tableRow2=0x7f090004;
  }
  public static final class layout {
    public static final int actionbar_view=0x7f030000;
    public static final int main=0x7f030001;
  }
  public static final class menu {
    public static final int main=0x7f080000;
  }
  public static final class string {
    public static final int action_settings=0x7f060002;
    public static final int app_name=0x7f060000;
    public static final int hello_world=0x7f060001;
    public static final int leftArrow=0x7f060004;
    public static final int rightArrow=0x7f060003;
  }
  public static final class style {
    /** 
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.


    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.

 API 11 theme customizations can go here. 

    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.

 API 14 theme customizations can go here. 
     */
    public static final int AppBaseTheme=0x7f070000;
    /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
     */
    public static final int AppTheme=0x7f070001;
}
}

因为您的目标是API级别11以上,所以应该替换

导入android.support.v7.widget.SearchView

导入android.widget.SearchView

另请参阅这篇关于操作栏搜索视图的文章


根据logcat,您的错误在于菜单文件
R.menu.main

改变

 android:icon="@+id/ic_action_search"


任何logcat信息??此行的代码是什么
MainActivity.java:64
请帮助任何人?Panther我刚刚添加了一个新日志请阅读it@Marta我刚刚添加了我的drawable,它显示了一个图标的未定义drawable状态,我如何解决它?蒙克布罗克先生,我做了你说的但没用,请帮帮我?@Marta非常感谢你们所有人试图帮助我,我真的很感激
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ADD8E6"
        android:text="@string/leftArrow" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ADD8E6"
        android:text="@string/rightArrow" />

   </TableRow>

   <TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:background="#ADD8E6"
    android:layout_weight="1"
    android:layout_height="wrap_content" >

  </TableRow>


  </TableLayout>
12-07 12:44:39.122: E/AndroidRuntime(1302): FATAL EXCEPTION: main
12-07 12:44:39.122: E/AndroidRuntime(1302): Process: com.example.folowreader, PID: 1302
12-07 12:44:39.122: E/AndroidRuntime(1302): android.content.res.Resources$NotFoundException: Resource "com.example.folowreader:id/ic_action_search" (7f090007)  is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f090007}
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.content.res.Resources.loadDrawableForCookie(Resources.java:2390)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.content.res.Resources.loadDrawable(Resources.java:2330)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.content.res.Resources.getDrawable(Resources.java:758)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.content.Context.getDrawable(Context.java:402)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:388)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.ActionMenuItemView.initialize(ActionMenuItemView.java:115)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.widget.ActionMenuPresenter.bindItemView(ActionMenuPresenter.java:185)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.BaseMenuPresenter.getItemView(BaseMenuPresenter.java:178)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.widget.ActionMenuPresenter.getItemView(ActionMenuPresenter.java:171)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.widget.ActionMenuPresenter.flagActionItems(ActionMenuPresenter.java:459)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.MenuBuilder.flagActionItems(MenuBuilder.java:1082)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.BaseMenuPresenter.updateMenuView(BaseMenuPresenter.java:87)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.widget.ActionMenuPresenter.updateMenuView(ActionMenuPresenter.java:208)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.MenuBuilder.dispatchPresenterUpdate(MenuBuilder.java:257)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.MenuBuilder.onItemsChanged(MenuBuilder.java:976)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.view.menu.MenuBuilder.startDispatchingItemsChanged(MenuBuilder.java:999    )
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:591)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:917)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:258)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.os.Handler.handleCallback(Handler.java:739)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.os.Looper.loop(Looper.java:135)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at java.lang.reflect.Method.invoke(Native Method)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at java.lang.reflect.Method.invoke(Method.java:372)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-07 12:44:39.122: E/AndroidRuntime(1302):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-07 12:44:42.584: I/Process(1302): Sending signal. PID: 1302 SIG: 9
/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

 package com.example.folowreader;

 public final class R {
 public static final class attr {
 }
 public static final class color {
    public static final int light_blue=0x7f040000;
 }
 public static final class dimen {
    /**  Default screen margins, per the Android Design guidelines. 

     Example customization of dimensions originally defined in res/values/dimens.xml
     (such as screen margins) for screens with more than 820dp of available width. This
     would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).

     */
    public static final int activity_horizontal_margin=0x7f050000;
    public static final int activity_vertical_margin=0x7f050001;
  }
  public static final class drawable {
    public static final int ic_action_search=0x7f020000;
    public static final int ic_launcher=0x7f020001;
  }
  public static final class id {
    public static final int action_settings=0x7f090005;
    public static final int button1=0x7f090002;
    public static final int button2=0x7f090003;
    public static final int ic_action_search=0x7f090007;
    public static final int search_action=0x7f090006;
    public static final int searchfield=0x7f090000;
    public static final int tableRow1=0x7f090001;
    public static final int tableRow2=0x7f090004;
  }
  public static final class layout {
    public static final int actionbar_view=0x7f030000;
    public static final int main=0x7f030001;
  }
  public static final class menu {
    public static final int main=0x7f080000;
  }
  public static final class string {
    public static final int action_settings=0x7f060002;
    public static final int app_name=0x7f060000;
    public static final int hello_world=0x7f060001;
    public static final int leftArrow=0x7f060004;
    public static final int rightArrow=0x7f060003;
  }
  public static final class style {
    /** 
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.


    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.

 API 11 theme customizations can go here. 

    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.

 API 14 theme customizations can go here. 
     */
    public static final int AppBaseTheme=0x7f070000;
    /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
     */
    public static final int AppTheme=0x7f070001;
}
}
 android:icon="@+id/ic_action_search"
 android:icon="@android:drawable/ic_menu_search"