将事件从一个片段发送到活动中的另一个片段

时间:2013-12-28 06:37:33

标签: java android android-fragments

我正在尝试将一个事件从一个片段发送到活动中的另一个片段

Fragment1.java

public class Fragment1 extends Fragment implements View.OnClickListener{

    Button BTN;
    Communicator comm;
    int count=0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.fragment1, container,false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        BTN=(Button) getActivity().findViewById(R.id.button_id);
        comm=(Communicator)getActivity();
        BTN.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        count++;
        comm.respond("clicked "+count+" times");

    }

}

Fragment2.java

public class Fragment2 extends Fragment{

    TextView ET;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view=inflater.inflate(R.layout.fragment2, container,false);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        ET=(TextView) getActivity().findViewById(R.id.textView_id);
    }

    public void ChangeText(String data)
    {
        ET.setText(data);
    }


}

MainActivity.java

public class MainActivity extends FragmentActivity implements Communicator{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void respond(String data) {
        // TODO Auto-generated method stub

        FragmentManager manager=getSupportFragmentManager();
        Fragment2 f2=(Fragment2) manager.findFragmentById(R.id.fragment2);
        f2.ChangeText(data);

    }


}

Communicator.java

public interface Communicator {

    public void respond(String data);

}

日志::

12-28 12:02:05.096: E/AndroidRuntime(394): FATAL EXCEPTION: main
12-28 12:02:05.096: E/AndroidRuntime(394): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fragmenttofragmentcommunication/com.example.fragmenttofragmentcommunication.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.os.Looper.loop(Looper.java:123)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-28 12:02:05.096: E/AndroidRuntime(394):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 12:02:05.096: E/AndroidRuntime(394):  at java.lang.reflect.Method.invoke(Method.java:521)
12-28 12:02:05.096: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-28 12:02:05.096: E/AndroidRuntime(394):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-28 12:02:05.096: E/AndroidRuntime(394):  at dalvik.system.NativeStart.main(Native Method)
12-28 12:02:05.096: E/AndroidRuntime(394): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-28 12:02:05.096: E/AndroidRuntime(394):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.Activity.setContentView(Activity.java:1647)
12-28 12:02:05.096: E/AndroidRuntime(394):  at com.example.fragmenttofragmentcommunication.MainActivity.onCreate(MainActivity.java:14)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-28 12:02:05.096: E/AndroidRuntime(394):  ... 11 more
12-28 12:02:05.096: E/AndroidRuntime(394): Caused by: java.lang.IllegalStateException: Fragment com.example.fragmenttofragmentcommunication.Fragment1 did not create a view.
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:314)
12-28 12:02:05.096: E/AndroidRuntime(394):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
12-28 12:02:05.096: E/AndroidRuntime(394):  ... 20 more

注意 ::我使用较低版本的android,所以我在所有地方使用了支持片段

1 个答案:

答案 0 :(得分:1)

在片段1中

 View view=inflater.inflate(R.layout.fragment1, container,false);
 return view;

然后

@Override
public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        BTN=(Button) getView().findViewById(R.id.button_id); 
        // if button is in fragment layout should us getView() instead of activity
        comm=(Communicator)getActivity();
        BTN.setOnClickListener(this);

}