我已经阅读了很多帖子,问题和答案,但我无法解决我的问题。它不工作。Android,按钮片段不工作
我有一个Android Studio的Mainmenu-Activity应用程序。现在我有一些碎片被设置到容器中(通过替换)。
现在我在Fragment中添加了一个按钮,并希望它能进行一些计算。但这是问题。这个Button没有做任何事情。
这里我的代码:
package eu.terratex.terratextoolbox;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.view.View.OnClickListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link Knastrechner.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link Knastrechner#newInstance} factory method to
* create an instance of this fragment.
*
*/
public class Knastrechner extends Fragment {
private OnFragmentInteractionListener mListener;
private Button btnClick;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Knastrechner.
*/
// TODO: Rename and change types and number of parameters
public static Knastrechner newInstance() {
Knastrechner fragment = new Knastrechner();
Bundle args = new Bundle();
return fragment;
}
public Knastrechner() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View view = null;
view = inflater.inflate(R.layout.fragment_knastrechner, container, false);
Button mButton = (Button) view.findViewById(R.id.berechnenButton);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
System.out.println("abc");
}
});
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}
}
我添加的onclick这里: @覆盖 公共查看onCreateView(LayoutInflater吹气,ViewGroup中容器, 捆绑savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View view = null;
view = inflater.inflate(R.layout.fragment_knastrechner, container, false);
Button mButton = (Button) view.findViewById(R.id.berechnenButton);
mButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
System.out.println("abc");
}
});
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
}
谁能帮助我?
哦谢谢你,一个简单的解决方案3小时搜索答案:P – 2014-09-01 13:42:37