2017-03-25 9 views
-1

파편이있는 앱을 만들고있어 버튼 클릭시 소리가납니다. 모든 것은 버튼을 클릭 할 때를 제외하고 작동합니다. 앱이 작동을 멈 춥니 다. 나는 이라는 이름의 onclick 메서드를 playAnother 버튼에 넣습니다. 나는 문제가 .java 파일에 있을지도 모른다라고 생각한다 : here is screen 나는 무엇을 해야할지 모른다. 나는 초보자이다. 좋은 하루 되세요!버튼 클릭 후 앱 작동 중지

+0

태그에 대한 참고 사항 : * JavaScript *는 코드 이미지를 게시하는 대신 * Java * –

+2

포스트 코드와 같지 않습니다. ! – rafsanahmad007

답변

0

방법 playAnother(View view)하지 조각에, 당신의 활동에 있어야합니다.

또는

당신이 당신의 버튼에 id 줄 수있는 당신의 조각에서 버튼 클릭을 처리하려면

:

<Button 
    android:id="@+id/button" 
    . 
    . 

그리고를 다음 조각onCreateView()

public View onCreateView(...) { 
    // First we save the reference to the views of your fragment 
    View view = inflater.inflate(R.layout.fragment_three, container, false); 

    // Then we need to find the button-view 
    Button button = (Button) view.findViewById(R.id.button); 
    // And finally we can register OnClickListener for the button 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Handle your button click here 
     } 
    }); 
    return view; 
} 
에서
+0

의 onClick 이벤트를 참조 할 수 없으므로 FragmentThree.java에서 MainActivity.java로 모든 것을 넣고 fragment_three.xml에 버튼을 남겨 두어야합니다. –

+0

어쩌면 그게 효과가 있을지 모르겠지만 그때 단편을 갖는 목적은 무엇입니까? 조각에서 클릭을 처리하는 것이 좋습니다. 단어 "OR" – TuomasK

+0

아래 내 조언을 따르십시오 정확히 내가 원하는 것입니다! 정말 고맙습니다 –

0

은 단추의 OnClick이 fragment_three.xml 레이아웃 설정되었습니다

<Button 
. 
. 
. 
android:onClick="playAnother" /> 
+0

예 fragment_three.xml에 있습니다 –

+0

http://stackoverflow.com/questions/18711433/button-listener-for-button-in-fragment-in-android 이것이 도움이 될 것입니다. 프래그먼트는 레이아웃 –