片段与主要活动之间的沟通

时间:2015-01-19 03:21:37

标签: android android-fragments

我正在尝试实现活动和片段之间的通信,但我无法弄清楚如何做到这一点。我一直收到以下错误:

01-19 16:15:01.838    6105-6105/com.example.torresj.unlockdemo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.torresj.unlockdemo.HomeFragment$1.onClick(HomeFragment.java:42)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            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:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

第42行是我的片段类的someEventListener.someEvent(final_str);。来源如下:

public class HomeFragment extends Fragment {

    public interface onSomeEventListener {
        public void someEvent(String s);
    }
    onSomeEventListener someEventListener;

    public HomeFragment() {}
    View rootView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_home, container, false);
        Button Send = (Button) rootView.findViewById(R.id.sendbtn);

        final EditText Username = (EditText) rootView.findViewById(R.id.editText);
        final EditText Password = (EditText) rootView.findViewById(R.id.editText2);

        Send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = Username.getText().toString();
                String s2 = Password.getText().toString();
                String final_str = s + ":" + s2;

                someEventListener.someEvent(final_str);
            }
        });

        return rootView;
    }

}

我也宣布我的主要活动为:

public class MainActivity extends ActionBarActivity implements HomeFragment.onSomeEventListener

我的主要活动的底部是ff函数:

@Override
    public void someEvent(String s) {
        String y = s;
        Log.i("Frag", s);
    }

我不确定为什么会产生错误。在我的片段类中调用someEventListener.someEvent(final_str);之前,该字符串是正确的。

1 个答案:

答案 0 :(得分:1)

您需要将侦听器设置为您的片段。将此代码添加到HomeFragment

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            someEventListener = (OnSomeEventListener) activity;
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Activity must implement HomeFragment.OnSomeEventListener!");
        }
    }

此外,正如 Eugen Pechanec 所建议的那样,清除onDetach中的引用以防止潜在的内存泄漏。

@Override
public void onDetach() {
    super.onDetach();
    someEventListener = null;
}