I have a little problem which is sending information from one fragment to another fragment. I created a project with swipe navigation. In one fragment I have only a button. In the other fragment there is only a textview. I want to send a text to fragment2 when I click the button in fragment1. I tried it with interfaces, but I don’t know what I am doing wrong.
Here are the current codes.
The interfaces:
public interface ActivityCommunicator {
public void passDataToActivity(String someValue);
}
public interface FragmentCommunicator {
public void passDataToFragment(String someValue);
}
The 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>
The logcat error message:
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
Any advices to fix that? Thanks in advance.
Could we see your
onClickListenerfor your button? It seems that it is calling the private methodsendTextof yourMainActivityedit: you did it in your XML file, my bad 😀 But I still think that the fact that the
SendTextmethod is private could be the problem.