当我从活动/片段发送文本到活动/片段时收到错误

时间:2013-01-24 14:32:26

标签: android android-fragments android-fragmentactivity

我有一个小问题,即将信息从一个片段发送到另一个片段。我用滑动导航创建了一个项目。在一个片段中,我只有一个按钮。在另一个片段中只有一个textview。当我点击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

有任何修复建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我们可以看到您的onClickListener按钮吗?它似乎正在调用sendText

私有方法MainActivity

编辑:你是在XML文件中做到的,我的不好:D但我仍然认为SendText方法是私有的这个问题可能就是问题。