Attempt to invoke virtual method? - android

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.

Related

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);

How to add onClickListener to the fragment [duplicate]

I have a sliding menu project and inside home layout another layout is called as a fragment :
this is the HomeFragment.java :
package info.androidhive.slidingmenu;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
return rootView;
}
}
I need to import this click listener inside my java class in order to submit a form .
//CheckFal:
btnCheckFalAction = (Button) findViewById(R.id.btnCheckFal);
btnCheckFalAction.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
}
But when I add the above code snippet it throws me an error on undefined methods such as findViewById , OnClickListener , onClick
The button is inside this layout fragment_home.xml
<Button
android:id="#+id/btnCheckFal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="#string/CheckFal" />
While working with Fragment, you have to inflate view.
So when you want to use any widget you have to use view object with findViewById().
Simply Do like this...
public class HomeFragment extends Fragment {
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
}
OR try other way..
public class HomeFragment extends Fragment implements OnClickListener{
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnCheckFal :
//your code...
break;
default:
break;
}
}
}
You can use
rootView.findViewById(...)
in your code, because your class doesn't inherit/implement this method
first...you should findView in rootView such as
btnCheckFalAction = (Button) rootView.findViewById(R.id.btnCheckFal);
then you can finish Button's operation
// Inflate the layout for this fragment in on create
RelativeLayout mLinearLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_a,
container, false);
cd = new ConnectionDetector(getActivity());
ImageButton mButton = (ImageButton) mLinearLayout.findViewById(R.id.imageButton1);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
// e.g. launch a new activity
Toast.makeText(getActivity(),"Opening Maps",Toast.LENGTH_LONG).show();
}
});
you can call this by view

How to make a lock screen button in fragment layout?

I want to made a lock screen Button in a Fragment Layout.
I searched some help and found the same, but made using an Activity.
The code is here:
http://karanbalkar.com/2014/01/tutorial-71-implement-lock-screen-in-android/
I don't know how to change it to work in a Fragment?
Please help
My code:
public class Tab1fragment extends Fragment {
/**
* #param args
*/
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.tab1_layout, container, false);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
you have to read more about Fragment: http://developer.android.com/reference/android/app/Fragment.html
first you have to implement View.OnClickListener in your Fragment, then create a View inside onCreateView like :
View rootView = inflater.inflate(R.layout. tab1_layout, container, false);
so your fragment became :
public class Tab1fragment extends Fragment implements View.OnClickListener {
.
.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1_layout, container, false);
mDevicePolicyManager = ...
mComponentName = ...
Button btnEnableAdmin = (Button) rootView.findViewById(R.id.btnEnable);
.
.
btnEnableAdmin.setOnClickListener(this);
.
.
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
...
}
}
}
NB: I tested this code on my device

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");
}
}

How to use Radio Groups in android Fragment activity

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 ...
}
}
}
}

Categories

Resources