繁体   English   中英

在Android App中单击按钮后,什么也没有发生

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM