-2

这个问题可能会重复,但我需要了解听众的工作原理。假设我有MyActivity()MyFragment()。当我按下返回按钮运行doStuff()片段类中的方法时,我需要创建一个监听器。我尝试过:

听众:

    public interface OnBackPressListener {
    void doBack();
    }

MyActivity 类中的代码:

 private OnBackPressListener onBackPressListener;

    ...   

       @Override
        public void onBackPressed() { 
            onBackPressedListener.doBack();
        }

MyFragment 类中的代码:

        public class MyFragment extends Fragment implements OnBackPressListener {

       ...

     @Override
        public void doBack() {
           doStuff();
        }

    private void doStuff(){
    //my work
    }


}

但我得到了 NullPointerException

09-04 05:34:04.637  13074-13074/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.ui.activity.MyActivity.onBackPressed(MyActivity.java:370)
            at android.app.Activity.onKeyUp(Activity.java:2159)
            at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
            at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:258)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
            at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
            at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
            at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
            at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
            at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
            at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:132)
            at android.os.Looper.loop(Looper.java:124)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

这 370 行是:

   onBackPressedListener.doBack();
4

1 回答 1

0

Just override the onBackPressed() method in your container activity:

     @Override
    public void onBackPressed() {
      //do your stuff here.....
       super.onBackPressed();
}
于 2015-09-04T09:27:34.453 回答