[英]Nothing happens when button is clicked in Android App
我無法讓按鈕在我正在處理的片段中工作。 這是代碼:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//create button listener for player match
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button mButton = (Button) rootView.findViewById(R.id.MatchButton);
Log.v("LOADING MATCH", "TEST");
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.v("LOADING MATCH", "TEST2");
Intent intent = new Intent(getActivity(), com.example.android.soccerstar.PlayerDisplay.class);
String message = "test";
intent.putExtra(intent.EXTRA_TEXT, message);
startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_main, container, false);
}
MatchButton在以下片段XML中定義:
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:text="Match to Player"
android:id="@+id/MatchButton"/>
該應用程序加載正常,但是當我單擊該按鈕時,什么也沒有發生。 有任何想法嗎? 我對此很陌生,所以請忍受。
謝謝!
將onCreateView
返回值更改為:
return rootView;
發生問題是因為為Button設置了單擊偵聽器,並從onCreateView
返回視圖對象都是不同的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.