简体   繁体   English

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

[英]Nothing happens when button is clicked in Android App

I'm having trouble getting a button to work in a fragment I'm working on. 我无法让按钮在我正在处理的片段中工作。 Here is the code: 这是代码:

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 is defined in the fragment XML here: 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"/>

The app loads fine, but when I click the button, nothing happens. 该应用程序加载正常,但是当我单击该按钮时,什么也没有发生。 Any Ideas? 有任何想法吗? I'm fairly new to this, so please bear with me. 我对此很陌生,所以请忍受。

Thanks! 谢谢!

Change return of onCreateView to : onCreateView返回值更改为:

return rootView;

Problem is occurring because setting click listener for Button and return view object from onCreateView both is different object's. 发生问题是因为为Button设置了单击侦听器,并从onCreateView返回视图对象都是不同的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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