如何通知Activity在片段中按下了后退按钮?

时间:2014-06-21 12:42:56

标签: android

我试图在触摸后退按钮时在片段中执行一些代码。我在我的活动中创建了一个界面,并在我的片段中实现它。但当我触摸它时,它会崩溃。我这样做的方式出了什么问题?

import android.support.v7.app.ActionBarActivity;

public class myActionBarActivity extends ActionBarActivity{
    public OnBackPressedListener onBackPressedListener;

    @Override
    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.actionbaractivity_layout); //loads fragment

    @Override
    public void onBackPressed(){        
        onBackPressedListener.onActivityBackPressed();

    }

    public interface OnBackPressedListener{
        public void onActivityBackPressed();    
    }   

}

片段:

import android.support.v4.app.Fragment;
public class myFragment extends Fragment implements myActionBarActivity.OnBackPressedListener{
    ...
    ...

    @Override
    public void onActivityBackPressed() {
        Toast.makeText(getActivity(), "Back pressed", Toast.LENGTH_SHORT).show();

    }

}

Logcat:

06-21 18:04:30.351: E/InputEventSender(6530): Exception dispatching finished signal.
06-21 18:04:30.351: E/MessageQueue-JNI(6530): Exception in MessageQueue callback: handleReceiveCallback
06-21 18:04:30.351: E/MessageQueue-JNI(6530): java.lang.NullPointerException
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at mypackage.myActionBarActivity.onBackPressed(myActionBarActivity.java:23)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.app.Activity.onKeyUp(Activity.java:2222)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.KeyEvent.dispatch(KeyEvent.java:2917)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.app.Activity.dispatchKeyEvent(Activity.java:2456)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchKeyEvent(ActionBarActivityDelegateICS.java:258)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2211)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4583)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4559)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4268)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4325)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4301)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4451)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2223)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1895)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1886)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2200)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.os.MessageQueue.nativePollOnce(Native Method)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.os.MessageQueue.next(MessageQueue.java:132)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.os.Looper.loop(Looper.java:124)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at android.app.ActivityThread.main(ActivityThread.java:5493)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at java.lang.reflect.Method.invokeNative(Native Method)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at java.lang.reflect.Method.invoke(Method.java:525)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-21 18:04:30.351: E/MessageQueue-JNI(6530):   at dalvik.system.NativeStart.main(Native Method)

这里解释了正确的方法https://stackoverflow.com/a/19133522/2105986。但我真的不明白。我正在寻找一种更简单的方法来实现它。

2 个答案:

答案 0 :(得分:4)

您的应用程序崩溃是因为您的Listener对象(onBackPressListener)为null。

在代码中的某处,您需要将实现OnBackPressListener接口的Fragment设置为Listener。

示例,片段:

public class MyFragment extends Fragment implements OnBackPressListener {
    //...
}

活动:

public class MyActivity extends Activity {

    private OnBackPressListener mListener;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        // assuming the fragment is defined in xml and has the id "myfrag"
        setContentView(R.layout.actionbaractivity_layout); 

        MyFragment f = (MyFragment) getFragmentManager().findFragmentById(R.id.myfrag);
        mListener = f;
    }

    @Override
    public void onBackPressed(){        
        mListener.onActivityBackPressed();   
    }
}

当然,在这种情况下,Listener是没用的,因为你可以简单地将Fragment本身存储为成员。或者,您可以为侦听器提供setter方法,以便可以从任何位置进行设置。

答案 1 :(得分:1)

根据@WonderCsabo的评论,

将此添加到活动:

public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener){
        this.onBackPressedListener = onBackPressedListener;
    }

将其添加到片段中的onResume()

((myActionBarActivity)getActivity()).setOnBackPressedListener(this);