我继续收到此消息,遗憾的是您的应用已停止而不知道原因
这是我的主要
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>
这是我的actionbar_view
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textFilter" >
</EditText>
</TableRow>
这是我的菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.folowreader.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item android:id="@+id/search_action"
android:icon="@+id/ic_action_search"
android:showAsAction="ifRoom"
android:title="Search"></item>
</menu>
这是我的主要布局
<?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>
我该如何解决?
这是我的logCat
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
这是我的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;
}
}
答案 0 :(得分:1)
根据logcat,您的错误位于菜单文件R.menu.main
更改
android:icon="@+id/ic_action_search"
到
android:icon="@android:drawable/ic_menu_search"
答案 1 :(得分:-1)
由于您的目标是API级别11以上,因此您应该替换
import android.support.v7.widget.SearchView;
通过
import android.widget.SearchView;
另见本文关于操作栏搜索视图。
http://tpbapp.com/android-development/android-action-bar-searchview-tutorial/