gpt4 book ai didi

java - 将事件从 Activity 中的一个 fragment 发送到另一个 fragment

转载 作者:行者123 更新时间:2023-12-01 13:40:43 27 4
gpt4 key购买 nike

我正在尝试将事件从 Activity 中的一个 fragment 发送到另一个 fragment

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,所以我在所有地方都使用了支持 fragment

最佳答案

在 fragment 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);

}

关于java - 将事件从 Activity 中的一个 fragment 发送到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20811807/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com