为什么我的ActionBar会在Galaxy S2上崩溃但不会在Galaxy S4上崩溃?

时间:2014-06-30 03:47:33

标签: android android-actionbar

我正在尝试在searchView上开发一个actionBar的应用。现在,在Galaxy S4上工作正常,但在星系S2上它正在崩溃。

也许是关于" ActionBarActivity"但我使用碎片。我不知道为什么会这样。这是我的活动:

public class SendSms extends Fragment implements OnClickListener, OnItemClickListener ,SearchView.OnQueryTextListener {

    public static final String LOG = "myLog";

    SearchView mSearchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        setHasOptionsMenu(true);
            v = inflater.inflate(R.layout.send_sms123, container,false);
            context = getActivity();

}

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.main, menu);


          inflater = context.getMenuInflater();

            MenuItem searchItem = menu.findItem(R.id.action_search);
             mSearchView = (SearchView) searchItem.getActionView();
             mSearchView.setOnQueryTextListener(this);
             mSearchView.setOnSearchClickListener(this);
             mSearchView.setQueryHint("Type name or number");


    menu.add("Load contact").setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            loadContactAgain();
            return false;
        }
    });
    menu.add("Clean contact").setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            DeleteAll();
            return false;
        }
    });
    //TEMP
    menu.add("Developer mode").setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            developerMode();
            return false;
        }
    });
        menu.add("Stop service").setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                context.stopService(new Intent(context,ServiceSender.class));
                return false;
            }
        });
        super.onCreateOptionsMenu(menu,inflater);
    }



    @Override
    public boolean onQueryTextSubmit(String query) {

    }

    @Override
    public boolean onQueryTextChange(String newText) {

        adapterContact.filter(newText);

        if(!adapterContact.isEmpty())
        setVisibiltyListBar(newText);
        return false;
    }

菜单/主:

<item android:id="@+id/action_search"
      android:title="Search"
      android:icon="@android:drawable/ic_menu_search"
      android:showAsAction="always"
      android:actionViewClass="android.widget.SearchView" />

这是LogCat:

06-30 06:40:52.485: E/AndroidRuntime(6036): FATAL EXCEPTION: main
06-30 06:40:52.485: E/AndroidRuntime(6036): java.lang.NullPointerException
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.bibas.timingsms.SendSms.onCreateOptionsMenu(SendSms.java:609)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1956)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:408)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:759)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:648)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:803)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.bibas.timingsms.SendSms.onCreateView(SendSms.java:104)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.View.measure(View.java:12863)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.View.measure(View.java:12863)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.View.measure(View.java:12863)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2256)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.View.measure(View.java:12863)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1197)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.os.Looper.loop(Looper.java:137)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at android.app.ActivityThread.main(ActivityThread.java:4507)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at java.lang.reflect.Method.invoke(Method.java:511)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-30 06:40:52.485: E/AndroidRuntime(6036):     at dalvik.system.NativeStart.main(Native Method)

triyng支持v7之后的新错误:

07-01 03:27:04.351: E/AndroidRuntime(2944): FATAL EXCEPTION: main
07-01 03:27:04.351: E/AndroidRuntime(2944): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bibas.timingsms/com.bibas.timingsms.Main}: java.lang.ClassNotFoundException: Didn't find class "com.bibas.timingsms.Main" on path: /data/app/com.bibas.timingsms-1.apk
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread.access$700(ActivityThread.java:159)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.os.Looper.loop(Looper.java:137)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread.main(ActivityThread.java:5419)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at java.lang.reflect.Method.invoke(Method.java:525)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at dalvik.system.NativeStart.main(Native Method)
07-01 03:27:04.351: E/AndroidRuntime(2944): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.bibas.timingsms.Main" on path: /data/app/com.bibas.timingsms-1.apk
07-01 03:27:04.351: E/AndroidRuntime(2944):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
07-01 03:27:04.351: E/AndroidRuntime(2944):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
07-01 03:27:04.351: E/AndroidRuntime(2944):     ... 11 more

1 个答案:

答案 0 :(得分:0)

如果您使用ActionBarActivity,则必须使用support-v7 SearchView

<item android:id="@+id/action_search"
    android:title="Search"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:actionViewClass="android.support.v7.widget.SearchView" />

然后您可以通过

检索它
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

确保更改SearchViewOnQueryTextListener

的导入