Reputation: 132
I have read a lot of posts and questions and answers but I can't resolve my problem. It is not working.
I have an App with the Mainmenu-Activity from the Android Studio. Now I have some Fragments that are set into the container (via replace).
Now I added a button into the Fragment and want that it will do some calculations. But here is the Problem. This Button is not doing anything.
Here my Code:
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);
}
}
I added the onclick here:
@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);
}
Can anyone help me?
Upvotes: 0
Views: 1954
Reputation: 19
in Fragment evrey thing after return view is null if no return view is written the all code written wont work
enter code h 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 view;
}ere
Upvotes: 0
Reputation: 18933
Change this from
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
to
return view;
because you have already inflated your layout in view
object.
Upvotes: 0
Reputation: 1691
You should do return view;
instead of doing return inflater.inflate(R.layout.fragment_knastrechner, container, false);
because otherwise you're just abandoning what you've done before in the code.
Upvotes: 2