2013-01-24 3 views
0

하나의 단편에서 다른 단편으로 정보를 보내는 약간의 문제가 있습니다. 스 와이프 탐색을 사용하여 프로젝트를 만들었습니다. 하나의 단편에는 버튼 만 있습니다. 다른 단편에는 텍스트 뷰만 있습니다. 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 :

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> 

로그 캣 오류 메시지 :

0,123,516
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 

해결 방법은 무엇입니까? 미리 감사드립니다.

+0

문서를 읽으십시오. android : Button javadoc의 onClick 설명 : "(...) 메서드가 public (...)이어야합니다." – njzk2

+0

thx njzk2, 이미 변경되었습니다. :) 버튼을 클릭하면 텍스트가 전송되지 않습니다. – user1953173

+0

fragmentCommunicator는 결코 초기화되지 않습니다 – njzk2

답변

0

귀하의 버튼에 대해 onClickListener을 볼 수 있습니까? 당신이 당신의 XML 파일에했다, 내 나쁜 : 당신의 MainActivity

편집의 개인 방법 sendText를 호출하는 것 같다 D를하지만 난 여전히 생각하는 그 SendText 방법은 문제가 될 수 개인이라는 사실 .

+0

안녕 psykhi, 게시물 주셔서 감사합니다. 당신이 맞아 xD 공개로 바꿨지 만 지금은 오류가 없지만 버튼을 클릭해도 아무것도하지 않습니다. 텍스트는 여전히 "큰 텍스트"입니다. – user1953173

+0

내 대답을 받아 들일 수 있겠습니까? Thansk;) 텍스트가 변경되지 않으면 if (fragmentCommunicator! = null) { fragmentCommunicator.passDataToFragment ("Hi from FragmentActivity"); }'fragmentCommunicator가 아마도 null입니다 :) – psykhi

관련 문제