我有一个小问题,即将信息从一个片段发送到另一个片段。我创建了一个带有滑动导航的项目。在一个片段中,我只有一个按钮。在另一个片段中只有一个文本视图。当我单击fragment1 中的按钮时,我想向fragment2 发送文本。我用接口尝试过,但我不知道我做错了什么。
这是当前的代码。
接口:
public interface ActivityCommunicator {
public void passDataToActivity(String someValue);
}
public interface FragmentCommunicator {
public void passDataToFragment(String someValue);
}
MainActivity
: _
public class MainActivity extends FragmentActivity implements ActivityCommunicator {
public FragmentCommunicator fragmentCommunicator;
SectionsPagerAdapter mSectionsPagerAdapter;
Button btn;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.button1);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
private void SendText(View v) {
if(fragmentCommunicator != null) {
fragmentCommunicator.passDataToFragment("Hi from FragmentActivity");
}
}
@Override
public void passDataToActivity(String someValue) {
// TODO Auto-generated method stub
}
}
Fragment1
:
public class Fragment1 extends Fragment {
Button btn;
public Fragment1(int position) {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmen1, container, false);
btn = (Button)v.findViewById(R.id.button1);
return v;
}
}
Fragment2
:
public class Fragment2 extends Fragment implements FragmentCommunicator {
TextView tv;
private ActivityCommunicator activityCommunicator;
private String activityAssignedValue ="";
public Fragment2(int position) {
// TODO Auto-generated constructor stub
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, container, false);
tv = (TextView)v.findViewById(R.id.textView1);
return v;
}
@Override
public void passDataToFragment(String someValue) {
activityAssignedValue = someValue;
tv.setText(someValue);
}
}
fragment1.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SendText"
android:text="Button" />
</LinearLayout>
fragment2.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
main.xml
:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
logcat 错误信息:
01-24 14:13:41.750: W/dalvikvm(1576): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-24 14:13:41.760: E/AndroidRuntime(1576): FATAL EXCEPTION: main
01-24 14:13:41.760: E/AndroidRuntime(1576): java.lang.IllegalStateException: Could not find a method SendText(View) in the activity class com.example.zz.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.view.View$1.onClick(View.java:3578)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.view.View.performClick(View.java:4084)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.view.View$PerformClick.run(View.java:16966)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.os.Handler.handleCallback(Handler.java:615)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.os.Looper.loop(Looper.java:137)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-24 14:13:41.760: E/AndroidRuntime(1576): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 14:13:41.760: E/AndroidRuntime(1576): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 14:13:41.760: E/AndroidRuntime(1576): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-24 14:13:41.760: E/AndroidRuntime(1576): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-24 14:13:41.760: E/AndroidRuntime(1576): at dalvik.system.NativeStart.main(Native Method)
01-24 14:13:41.760: E/AndroidRuntime(1576): Caused by: java.lang.NoSuchMethodException: SendText [class android.view.View]
01-24 14:13:41.760: E/AndroidRuntime(1576): at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-24 14:13:41.760: E/AndroidRuntime(1576): at java.lang.Class.getMethod(Class.java:915)
01-24 14:13:41.760: E/AndroidRuntime(1576): at android.view.View$1.onClick(View.java:3571)
01-24 14:13:41.760: E/AndroidRuntime(1576): ... 11 more
有什么建议可以解决这个问题吗?提前致谢。