Android app keeps crashing after adding a specific line of code - java

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

Related

References in code moved from Activity to Fragment no longer resolve

I'm quite new working with Android/Java, so please bear with me.
I moved code from LoginActivity > onCreate into a fragment I created FragmentLogin to method onCreate where many classes no longer resolve, such as findViewById. I'm assuming that somewhere I didn't pass the context of the container Activity properly.
Or some other newbie mistake...
Here is LoginActivity.java [relevant parts copied]
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize Fragments //
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
setContentView(R.layout.fragment_login);
}
}
and FragmentLogin.java:
public class FragmentLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) findViewById(R.id.login_email);
final EditText textPassword = (EditText) findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) findViewById(R.id.button_login);
final Button buttonSignup = (Button) findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.fragment_forgotten_password);
}
});
... more code ...
}
}
The variables/methods that were working when the second block of code was residing in the onCreate method of the Activity but no longer resolve after I moved the code to onCreateView of the FragmentLogin fragment class:
findViewById, setContentView
Basically, this is a login form where the default fragment should be login, and a button on that page (Button forgottenPassword) would open another fragment (FragmentForgottenPassword).
Can anyone tell me what I'm doing wrong?
In your activity layout xml file, add something similar to this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="your.package.FragmentLogin"
android:id="#+id/fragment_login"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
In your LoginActivity: remove the fragment manager stuff. You don't need it yet.
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
}
}
In your FragmentLogin, move the return statement to the end and use the inflated view to find your views by id:
public class FragmentLogin extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final view view = inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) view.findViewById(R.id.login_email);
final EditText textPassword = (EditText) view.findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) view.findViewById(R.id.button_login);
final Button buttonSignup = (Button) view.findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.fragment_forgotten_password);
}
});
... more code ...
return view;
}
}
You should get subviews from layout while it's being inflated. The layout usually inflated in OnCreateView method.
View layout = inflater.inflate( R.layout.fragment_login, null );
final EditText textEmail = (EditText) layout.findViewById(R.id.login_email);
final EditText textPassword = (EditText) layout.findViewById(R.id.login_pass);
...
return layout;
I am guessing you want to the fragment to be shown within the activity. Try this:
public class LoginActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//put the setContentView() before the fragment initialization also, I noticed your activity layout is the same as your fragment layout,this should not be so, hence I changed that
setContentView(R.layout.activity_login);
// Initialize Fragments //
//you did not initialize the fragments completely, it should be like
//this
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction().replace(R.id.fragment_container, new FragmentLogin()).commit();
}}
then in the Login fragment, it should be like this:
public class FragmentLogin extends Fragment {#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_login, container, false);
final ValidationManager Check = new ValidationManager();
final SessionManager Session = new SessionManager(this);
final EditText textEmail = (EditText) view.findViewById(R.id.login_email);
final EditText textPassword = (EditText) view.findViewById(R.id.login_pass);
final Button buttonLogIn = (Button) view.findViewById(R.id.button_login);
final Button buttonSignup = (Button) view.findViewById(R.id.button_signup);
// Listen for FORGOTTEN PASSWORD click event, open ForgottenPassword Fragment //
final Button forgottenPassword = (Button) view.findViewById(R.id.button_lost_pass);
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//you cant call setContentView in a fragment
}
});
... more code ...
}}
I am not totally sure I caught all your mistakes but I think you should read these: one and two; they are online tutorials on fragments, you can also browse for more. I think it is a good starting point. All the best

Android Studio - Fragment button action

I tried to make an application that shows a fragment at start then you can change that fragment to an other one with a button in the first fragment. But when I put the button action in the fragments java, it won't start and I get the nullpointerexception error. Could you tell me why?
public class Fragment_main extends Fragment {
FragmentManager fm = getFragmentManager();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button_inditas = (Button) getView().findViewById(R.id.button_inditas);
button_inditas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fm.beginTransaction().replace(R.id.content_frame, new Fragment_1()).commit();
}
});
return inflater.inflate(R.layout.fragment_main,container,false);
}
}
But when I put the button action in the fragments java, it won't start and I get the nullpointerexception error. Could you tell me why?
Well I see some errors on your code...
First of all you can't call getView() if you haven't inflated one, it means that you MUST inflate a view as i'll put on my answer and then you can avoid the getView() and use that view itself.
And instead of returning the inflater you have to return your View
This is how your Fragment should look like:
public class Fragment_main extends Fragment {
public Fragment_main() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,container,false);
Button button_inditas = (Button)rootView.findViewById(R.id.button_inditas);
button_inditas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager ();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction ();
fragmentTransaction.add (R.id.content_frame, new Fragment_1());
fragmentTransaction.commit ();
});
return rootView;
}
}
You have to inflate the view first and use the view returned by that instead of getView ().
View view = inflater.inflate (...);
and then
... = (Button) view.findView(...);
This happens because the view that is returned by getView () hasn't been created yet, so getView() returns null.

#override error showing up in .xml file

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.

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