#override error showing up in .xml file - java

I've just moved my button onCreatView from my activity_main.java file to the correct file - my ConvertFragment.java file.
Now that I've copy and pasted the code, I'm getting a red-x errors on all of my findViewById's... any thoughts?
thanks in advance!
public class ConvertFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_convert, container, false);
final EditText editDecimal = (EditText) findViewById(R.id.editDecimal);
final EditText editBinary = (EditText) findViewById(R.id.editBinary);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
int decimal = Integer.valueOf(editDecimal.getText().toString());
editBinary.setText(Integer.toBinaryString(decimal));
}
});
return rootView;
}
}

It's because a fragment doesn't have a method findViewById().
You can either call that method on your activity:
getActivity().findViewById()
Or just use the layout that you've just inflated and find view's inside of it:
rootView.findViewById()
The 2nd one is better (IMO) as it doesn't rely on the activity.

Related

I have added button to Fragment, now i am trying to register that button to Javafile of fragment, but its giving me error in java file

public class SlideshowFragment extends Fragment {
private FragmentSlideshowBinding binding;
public Button btn1,btn2;
#Override
public View onCreateView( LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
btn1=(Button) findViewById (R.id.button); // find view by id is giving error
(Cannot resolve method 'findViewById' in 'SlideshowFragment)
btn2=(Button) findViewById (R.id.button2);
SlideshowViewModel slideshowViewModel =
new ViewModelProvider(this).get(SlideshowViewModel.class);
binding = FragmentSlideshowBinding.inflate(inflater, container, false);
View root = binding.getRoot();
// final TextView textView = binding.button;
//slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
return root;
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
If you're using view binding you don't need to access your components with findViewById method. While using view binding, pressing dot (.) after calling your view binding reference will show you all available components in your layout. You can simply use:
btn1 = binding.button;
btn2 = binding.button2;
But if you still want to access your components with findViewById method you should use it with your root view reference:
btn1 = binding.getRoot().findViewById(R.id.button);
btn2 = binding.getRoot().findViewById(R.id.button2);

Android app keeps crashing after adding a specific line of code

I am using a navigation bar which is made up of fragments, and whenever I open up the fragment below, the app crashes
public class FirstFragment extends Fragment{
View myView;
private RadioGroup radioGroup;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(first_layout, container, false);
addListenerOnButton();
return myView;
}
public void addListenerOnButton() {
radioGroup = (RadioGroup) getView().findViewById(R.id.radio);
}
}
Now I did some experimenting to find the problem, and it turns out to be this line
radioGroup = (RadioGroup) getView().findViewById(R.id.radio);
Could anyone explain how to fix it? Thank you
try this:
addListenerOnButton(myView);
And in addListenerOnButton() method use:
public void addListenerOnButton(View v) {
radioGroup = (RadioGroup) v.findViewById(R.id.radio);
}

Nullpointerexception thrown when trying to findViewById

I have the following Activity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StartFragment())
.commit();
}
Button login = (Button) findViewById(R.id.loginButton);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
});
}
I get a NPE when I try to invoke findViewByID for R.id.loginButton, and I'm guessing this is because loginButton is within a separate Fragment, which I have as:
public static class StartFragment extends Fragment {
public StartFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
However, I am unsure of how to fix this so that I can find the loginButton ID. I haven't worked with fragments before, so I realize I may be using them/implementing them incorrectly. fragment_main contains a few buttons in a LinearLayout, and activity_main has nothing but a single FrameLayout.
Try to implement your onCreateView(...) in Fragment like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
View something = rootView.findViewById(R.id.something);
something.setOnClickListener(new View.OnClickListener() { ... });
return rootView;
}
The Button is in the fragment layout (fragment_main.xml) and not in the activity layout (activity_main.xml). onCreate() is too early in the lifecycle to find it in the activity view hierarchy, and a null is returned. Invoking a method on null causes the NPE.
Write code to initialize button from fragment becuase your button is into fragment layout not into activity's layout.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button login = (Button) rootView.findViewById(R.id.loginButton);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,
LoginActivity.class);
startActivity(intent);
}
});
return rootView;
}
And remove the login button related code from onCreate of Activity.
findViewById() works with reference to a root view.
Without having a view in the first place will throw a null pointer exception
In any activity you set a view by calling setContentView(someView);.
Thus when you call findViewById() , its with reference to the someView.
Also findViewById() finds the id only if its in that someView. So in you case null pointer exception
For fragments, adapters, activity, .... any view's findViewById() will only find if the id exixts in the view
Alternately if you are inflating a view, then you can also use inflatedView.findViewById() to get a view from that inflatedView
In short make sure you have the id in your layout you are referring to or make findViewById() call in appropriate place(Ex. adapters getView(), activity's onCreate() or onResume() or onPause() , fragments onCreateView(), ....)
Also have an idea about UI & background thread's as you cannot efficiently update UI in bg-threads

SlidingMenu in Child Activities Navigation Android

I am trying to add left side slide menu inside my application but i am facing little trouble getting the slide menu to the following child activities of the fragment.
So far i have tried using this sliding menu example and when i got this image now inside the "Find People" fragment class i have static data.
Find People (Parent Activity) on Button Click go to ---> Friends (Child Activity of Find People) on Button Click go to ----> Names (Child Activity of Friends)
I am trying to get the same sliding menu with the Child Activities also that i failed to achieve so far please can any one help me out
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Friends.class);
startActivity(intent);
}
});
return rootView;
}
}
Friends (Child Activity)
public class Friends extends Activity {
Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.friends_xml);
btnNext = (Button)findViewById(R.id.btnNexts);
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Go to Names Child Activity
}
});
}
}
And Similarly Names (Child Activity)
How do i get the same slide menu in both the child activities (Friends & Names). Any idea ??
Plz help me out in this.
Code Snippet will be much helpful.
Thanks
As #Piyush Gupta said you need to call new Fragment instead of calling activities,
In Place of activity you need to use Fragment and move from one Fragment to next used this code
Instead of using Fragment element use Framelayout id of the fragment Xml
Replace ur code with this
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
Button btn = (Button) rootView.findViewById(R.id.butto);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragementDemo fd = new FragementDemo();
android.support.v4.app.FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fd); // content_frame is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
return rootView;
}
}
And your next child activity will extend Fragment not Activity like this :
public class Friends extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragementdemo, container, false);
// use button click code as used in FindPeopleFragment
return rootView;
}
}
Hope it works fine now for you!!

Fragment not calling onClick

I've got a Fragment implementing onClickListener. What I have is a custom dialer which must show on the edittext the introduced number. But it doesn't show anything.
This is the Fragment:
PhoneView.java
public class PhoneView extends Fragment implements OnTabChangeListener, OnClickListener {
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.phone_view, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
...
ImageButton button1 = (ImageButton) getView().findViewById(R.id.one);
EditText numTxt = (EditText) getView().findViewById(R.id.digits);
...
button1.setOnClickListener(this); //UPDATED - ADDED
...
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.one:
numTxt.setText(numTxt.getText()+"1");
break;
...
you have to call View.setOnClickListener(OnClickListener) of the view you want to react on the click event
In your Activity class, you need to define your Phoneview as you ClickListener to the button or another "view"
Suppose in your activity class, u have a button named deleteButton, and if u want to register a listener for this button... This is how you would do it.
private Button deleteButton= null;
deleteButton.setOnClickListener(PhoneView);

Categories