3

ただし、SherlockListActivity のアクション バーに検索機能を追加していますが、常に null の SearchView をインスタンス化しようとすると、次のようになります。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main_menu, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.activity_main_menu_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            public boolean onQueryTextSubmit(String p1){
                List<Application> auxList = Search.search(mContext, p1);
                AdapterApp aux = new AdapterApp(mContext, auxList);
                setListAdapter(aux);
                getListView().setOnItemClickListener(aux.getItemClickListener());
                return true;
            }

            public boolean onQueryTextChange(String p1){
                return false;
            }           
    });
    return super.onCreateOptionsMenu(menu);
}

activity_main_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/activity_main_menu_search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="ifRoom|collapseActionView"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView " />

    <item android:id="@+id/activity_main_menu_purchase"
        android:title="@string/purchase"
        android:icon="@drawable/ic_launcher_market_holo"
        android:showAsAction="ifRoom|collapseActionView" />

</menu>

また、必要なクラスを正しくインポートしたと言わざるを得ません。

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnActionExpandListener;
import com.actionbarsherlock.widget.SearchView;

そしてこれがスタックトレースです。SearchView がインスタンス化されると、ログは次のように返します。

06-03 19:42:43.128: W/dalvikvm(15936): dvmFindClassByName rejecting 'com.actionbarsherlock.widget.SearchView '
06-03 19:42:43.138: W/MenuInflater(15936): Cannot instantiate class: com.actionbarsherlock.widget.SearchView 
06-03 19:42:43.138: W/MenuInflater(15936): java.lang.ClassNotFoundException: Didn't find class "com.actionbarsherlock.widget.SearchView " on path: /data/app/com.iamaner.oneclickfreeze-1.apk
06-03 19:42:43.138: W/MenuInflater(15936):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.newInstance(MenuInflater.java:486)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.setItem(MenuInflater.java:447)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater$MenuState.addItem(MenuInflater.java:468)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater.parseMenu(MenuInflater.java:190)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.view.MenuInflater.inflate(MenuInflater.java:112)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.iamaner.oneclickfreeze.ActivityMain.onCreateOptionsMenu(ActivityMain.java:131)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.app.SherlockListActivity.onCreatePanelMenu(SherlockListActivity.java:184)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.actionbarsherlock.app.SherlockListActivity.onCreateOptionsMenu(SherlockListActivity.java:149)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:458)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:820)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:248)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer.doFrame(Choreographer.java:531)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Handler.handleCallback(Handler.java:725)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.os.Looper.loop(Looper.java:137)
06-03 19:42:43.138: W/MenuInflater(15936):  at android.app.ActivityThread.main(ActivityThread.java:5231)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:42:43.138: W/MenuInflater(15936):  at java.lang.reflect.Method.invoke(Method.java:511)
06-03 19:42:43.138: W/MenuInflater(15936):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)

次に、SearchView が null なので、SearchView.setOnQueryTextListener(); を呼び出すと NullPointerException が発生します。

06-03 19:42:43.148: E/AndroidRuntime(15936): FATAL EXCEPTION: main
06-03 19:42:43.148: E/AndroidRuntime(15936): java.lang.NullPointerException
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.iamaner.oneclickfreeze.ActivityMain.onCreateOptionsMenu(ActivityMain.java:133)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.app.SherlockListActivity.onCreatePanelMenu(SherlockListActivity.java:184)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:559)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:65)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.actionbarsherlock.app.SherlockListActivity.onCreateOptionsMenu(SherlockListActivity.java:149)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.app.Activity.onCreatePanelMenu(Activity.java:2490)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:458)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:820)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:248)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer.doFrame(Choreographer.java:531)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Handler.handleCallback(Handler.java:725)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.os.Looper.loop(Looper.java:137)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at android.app.ActivityThread.main(ActivityThread.java:5231)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at java.lang.reflect.Method.invoke(Method.java:511)
06-03 19:42:43.148: E/AndroidRuntime(15936):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)

エラーの原因を探して一日中無駄にしましたが、成功しません...助けが必要です

4

3 に答える 3

9

エラー自体は ActionBarSherlock ライブラリが原因です。GitHub で問題を見つけることができます: https://github.com/JakeWharton/ActionBarSherlock/issues/685

ここに投稿されたコメントは私の解決策でした: https://github.com/JakeWharton/ActionBarSherlock/issues/685#issuecomment-11670533

ABS ライブラリ プロジェクトの res/values-v14/abs_themes.xml に移動します。次に、最初の 2 つのテーマを次のように編集する必要があります。

<style name="Sherlock.__Theme" parent="android:Theme.Holo">
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
</style>
<style name="Sherlock.__Theme.Light" parent="android:Theme.Holo.Light">
    <item name="textColorPrimary">@color/abs__primary_text_holo_light</item>
    <item name="textColorPrimaryInverse">@color/abs__primary_text_holo_dark</item>
</style>

また、それを使用するアクティビティのマニフェストで宣言することを忘れないでください。

android:theme="@style/Theme.Sherlock.Light"
于 2013-06-03T18:37:27.300 に答える
2

I'm not really familiar with the usage of the ActionBarSherlock SearchView class, but I can tell you is that if you look at the samples, he doesn't try to get the SearchView from the menu, but rather he creates the SearchView and sets it to the menu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;

    //Create the search view
    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Search for countries…");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);

    if (mSuggestionsAdapter == null) {
        MatrixCursor cursor = new MatrixCursor(COLUMNS);
        cursor.addRow(new String[]{"1", "'Murica"});
        cursor.addRow(new String[]{"2", "Canada"});
        cursor.addRow(new String[]{"3", "Denmark"});
        mSuggestionsAdapter = new SuggestionsAdapter(getSupportActionBar().getThemedContext(), cursor);
    }

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    return true;
}
于 2013-06-03T18:12:19.690 に答える
0

上記のgithubの問題に関するこのコメントは、私から解決しました。(テーマのベースを削除するだけなので、奇妙な修正です。)

于 2013-10-27T10:56:12.797 に答える