How to use Radio Groups in android Fragment activity - android

I am trying to implement Radio groups in my SherlockFragment activity....but not able to do this.I am getting "java.lang.NullPointer" Exception when i run the application and no activity is displayed on the screen.
here is my code
public class Fragment_1 extends SherlockFragment {
RadioGroup cls;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View V= inflater.inflate(R.layout.fragment_1, container, false);
cls= (RadioGroup) V.findViewById(R.id.radioclass);
cls.clearCheck();
OnClickListener listener = new OnClickListener(){
/#Override
public void onClick(View V) {
// TODO Auto-generated method stub
RadioButton rb = (RadioButton) V;
clss=rb.getText().toString();
}
};
RadioButton rb1 = (RadioButton)V. findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
RadioButton rb2 = (RadioButton)V. findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener);
rb2.setChecked(true);
return V;
}
}

I don't think I would be doing you any favours by posting a cut and paste solution, but this might be
helpful (don't be too surprised if you find a few errors in it):
public class Fragment_1 extends SherlockFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// inflate ...
View view = inflater.inflate(R.layout.fragment_1, container, false);
return view;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Button rb1 = (Button) getActivity().findViewById(R.id.radioButton1);
//then do other buttons ...
rb1.setOnClickListener(next_Listener);
//then do other buttons ...
}
private OnClickListener next_Listener = new OnClickListener() {
public void onClick(View v) {
//xml find out which radio button has been checked ...
RadioGroup radio_grp=(RadioGroup)getActivity().findViewById(R.id.radio_grp); //change or leave out this line (I've put it in because you might find it useful later )
RadioButton rb1=(RadioButton)getActivity().findViewById(R.id.radioButton1); //you dont need to do this again if global ...
if(rb1.isChecked() == true) {
//toast ... button has been selected ...
}
}
}
}

Related

Making stopwatch/randomizer app but it crashes everytime I try to use one of those two

So, I wanted to make an app that has the main activity in which I can switch between two fragments containing a number randomizer and the other one a stopwatch. The stopwatch, meanwhile, must not stop if I switch to another fragment (it may only stop after I click again a start/stop button). I can switch between them normally, but when I try to generate a random number via button or I try to start the stopwatch, the app crashes. This is what I tried:
Stopwatch fragment:
public class frStopwatch extends Fragment {
View view;
EditText etTime;
Button btnStartStop;
Button btnReset;
Chronometer chronometer;
private boolean running;
private long pauseOffset;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_stopwatch, container, false);
btnStartStop = (Button) view.findViewById(R.id.btnStartStop);
btnReset = (Button) view.findViewById(R.id.btnReset);
btnStartStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.start();
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
return view;
}
}
Randomizer fragment:
public class frRandomizer extends Fragment {
View view;
Button btnRandom;
EditText etShowNum;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_fr_randomizer, container, false);
btnRandom = view.findViewById(R.id.btnRandom);
etShowNum = view.findViewById(R.id.etShowNum);
btnRandom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Random r = new Random();
etShowNum.setText(r.nextInt(9));
}
});
return view;
}
}
The app crashes on both emulator and mobile phone.

Beginner trying out onClickListener in fragment. It keep crashing in runtime

I am new to Java/android develop and trying to follow instructions from android site but cannot find clear answer to implementing onclicklistener in fragment. Please help me why its keep crashing in runtime when I click.
public class TestFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
try this
use this
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
instead of this
TextView TextviewShow = (TextView)getActivity().findViewById(R.id.text_show);
change your code as below
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
return v;
}
Try this
public class TestFragment extends Fragment {
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_mngr_user, container, false);
return v;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDBhelper = new UserBaseHelper(getActivity());
// Capture our button from layout
TextView TextviewShow = (TextView)v.findViewById(R.id.text_show);
// Register of onClick listener with the implementation above
TextviewShow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something when the text_show is clicked
displayInfo();
}
});
}
.....
}
Move your code from onCreate() method to onCreateView() method and change the following code
From
(TextView)getActivity().findViewById(R.id.text_show);
to
(TextView)v.findViewById(R.id.text_show);

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}

Attempt to invoke virtual method?

I was trying to implement onClick listener in fragments.
when i add
button1.setOnClickListener (this)
is getting force close.
Fragment code
Stack Trace
private class PlaceholderFragment extends Fragment implements OnClickListener{
Button b1;
private PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
b1 = (Button) container.findViewById(R.id.button1);
b1.setOnClickListener(PlaceholderFragment.this);
return rootView;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
This
b1 = (Button) container.findViewById(R.id.button1);
Should be
b1 = (Button) rootView.findViewById(R.id.button1);
You inflate the layout and you need to use the view object to initialize views.

android PopupWindow from a fragment

I have an app that uses fragments, its working ok,
but I have now to implement some pop ups when a button gets tapped,
I am following this tutorial "Example of using PopupWindow"
But I get this errors:
Multiple markers at this line
- LAYOUT_INFLATER_SERVICE cannot be resolved to a variable
- The method getBaseContext() is undefined for the type new
View.OnClickListener(){}
here my .java
public class Tab2HeadH1 extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //ERRORS HERE!!
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
SO, how can I fix this problem??, to show my pop up from tapped button in my fragment??
call
LayoutInflater layoutInflater = (LayoutInflater)**getActivity()**.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
Atleast it won't show error onBaseContext() as now it take from the associated activity
EDIT
Try this,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myFragmentView = inflater.inflate(R.layout.yourlayout, container,false);
//inside button onclick write the code given below
View popupView = inflater.inflate(R.layout.address, null);
}

Categories

Resources