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:
<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 :
return rootView;
Problem is occurring because setting click listener for Button and return view object from onCreateView
both is different object's.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.