这是另一种替代方法。这对我有用。
public class **YourFragmentClass** extends Fragment {
Context context; //Declare the variable context
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Pass your layout xml to the inflater and assign it to rootView.
View rootView = inflater.inflate(R.layout.**yourfragmentxml**, container, false);
context = rootView.getContext(); // Assign your rootView to context
Button **yourButton** = (Button) rootView.findViewById(R.id.**your_button_id**);
**yourButton**.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Pass the context and the Activity class you need to open from the Fragment Class, to the Intent
Intent intent = new Intent(context, **YourActivityClass**.class);
startActivity(intent);
}
});
return rootView;
}
}