android:尝试调用虚拟方法“ void android.view.View.unFocus(android.view.View)”

时间:2018-08-16 12:04:34

标签: android

我在项目中遇到此错误,我无法理解问题,我所知道的只是它是NPE,但我在崩溃日志中看不到任何Java文件。

我到处都在搜索这种错误,但是我找不到具体的解决方案。

请有人帮我解决这个错误。

我正在下面的logcat中发布崩溃日志。

 Crashed: main
           at android.view.ViewGroup.removeViewInternal(ViewGroup.java:5270)
           at android.view.ViewGroup.removeViewAt(ViewGroup.java:5233)
           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1771)
           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
           at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
           at android.support.v4.app.FragmentManagerImpl.dispatchPause(FragmentManager.java:3216)
           at android.support.v4.app.Fragment.performPause(Fragment.java:2527)
           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1485)
           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
           at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:851)
           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2593)
           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
           at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
           at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:854)
           at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:797)
           at ca.dataready.smartparent.HomeActivity.onBackPressed(HomeActivity.java:724)
           at android.app.Activity.onKeyUp(Activity.java:3150)
           at android.view.KeyEvent.dispatch(KeyEvent.java:2779)
           at android.app.Activity.dispatchKeyEvent(Activity.java:3481)
           at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
           at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
           at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
           at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
           at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:415)
           at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5191)
           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5063)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4727)
           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4608)
           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4784)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4608)
           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4581)
           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4634)
           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4600)
           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4760)
           at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4921)
           at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2599)
           at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2160)
           at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2151)
           at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2574)
           at 
android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
           at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
           at android.os.MessageQueue.next(MessageQueue.java:379)
           at android.os.Looper.loop(Looper.java:144)
           at android.app.ActivityThread.main(ActivityThread.java:7425)
           at java.lang.reflect.Method.invoke(Method.java)
           at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

编辑-

我已经查看了崩溃日志,发现它正在onBackpressed中发生

这是代码

 @Override
    public void onBackPressed() {

        FragmentManager fm = getSupportFragmentManager();
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else if (fm.getBackStackEntryCount() > 0) {
            Log.i("MainActivity", "popping backstack");
            getSupportFragmentManager().popBackStackImmediate();
        } else {
            Log.i("MainActivity", "nothing on backstack, calling super");
            super.onBackPressed();
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为您不见了,尝试尝试更改

       @Override
public void onBackPressed() {


    Fragment fragment = null;
    if(view == findViewById(R.id.YOURID)){
              fragment = new SOMEFragmentOne();
    } else {
              fragment = new SOMEFragmentTwo();                 
    }

    FragmentManager fm = getSupportFragmentManager();
                FragmentTransaction transaction = fm.beginTransaction();
                transaction.replace(R.id.output, fragment);
                transaction.commit();
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        getSupportFragmentManager().popBackStackImmediate();
    } else {
        Log.i("MainActivity", "nothing on backstack, calling super");
        super.onBackPressed();
    }



}

SOMEFragmentOne SOMEFragmentTwo 是扩展Fragment的两个类。