Null Pointing when adding to a fragment's layout - android

I am getting a Null Pointer Exception when trying to add a textView programmatically to a fragment.
I think the context is coming back null but I could be wrong. The crash is happening at startMenu.addView(tv);
public class TabFragment1 extends Fragment {
View inflatedView;
Context context = null;
private Bundle mBundle;
private LinearLayout startMenull ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if (inflatedView != null) {
ViewGroup parent = (ViewGroup) inflatedView.getParent();
if (parent != null)
parent.removeView(inflatedView);
}
try {
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
//startMenu = (LinearLayout) getActivity().findViewById(R.id.start_menull);
startMenu = (LinearLayout) getLayoutInflater(mBundle).inflate(R.id.start_menull, null);
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenu.addView(tv);
} catch (InflateException e) {
}
return inflatedView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
}
Updated above code 9/16 3:48
Solved:
in the try/catch
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
startMenull =((LinearLayout) inflatedView.findViewById(R.id.start_menull));
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenull.addView(tv);

context cant be null at that point, since he is using applicationContext.
but start Menu IS! null since onCreate is called before onCreateView. So you would have to inflate the View first, which you should do in onCreateView not onCreate.
Just Noticed you are inflating the View in onCreateView, but still you cant Access it in onCreate as it is called before onCreateView.
See Android Life Cycle. http://developer.android.com/images/fragment_lifecycle.png
public class TabFragment1 extends Fragment {
View inflatedView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.start_menu, container,false);
LinearLayout startMenu = (LinearLayout) inflatedView.findViewById(R.id.start_menu);
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setText("Here is the text view");
startMenu.addView(tv);
return inflatedView;
}
}
If i wanted to do something like you are doing, this would b how i'd do it. Why would u check if container is null? or why would you assume that inflatedView is != null?

onCreate calls before onCreateView. Read about lifecycle.

Related

trying to change the text of edittext object inside a fragment

I have a fragment that containd a EditText. Usally I get the address of the EditTExt by calling findViewById to get the object, then I call setText to chnage the text.
When I call findViewById, I get a error saying
"cannot resolve methed findViewById. I'm assuming the Fragment class does not support this methed.
HHow can I chnage the text?
My code
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EditText editText;
editText = (TextView)(findViewById(R.id.textViewVol));
editText.setText("% Volumne");
}
Try this:
You have to pass the instance of view of the fragment that you instantiate in your onCreateView method:
private View mView;
private EditText editText;
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
if (mView != null) {
ViewGroup group = (ViewGroup) mView.getParent();
if (group != null) {
group.removeView(mView);
}
} else {
//Inflate layout for this fragment
mView = layoutInflater.inflate(R.layout.frgament_layout, container, false);
edittext = (TextView)(mView.findViewById(R.id.textViewVol));
editText.setText("% Volumne");
return mView;
}

Can't inflate layout properly for a fragment in Android application

In first place this is the code of the fragment:
public class MyFragment extends Fragment {
private View FragmentView;
public static Fragment newInstance() {
MyFragment NewFragment = new MyFragment();
return NewFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentView = inflater.inflate(
R.layout.tasks,
new LinearLayout(MainActivity.MainContext)
);
FragmentView.setLayoutParams(
new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
);
addText();
return FragmentView;
}
private void addText() {
TasksView.setBackgroundResource(R.drawable.bg_image);
TextView MyTitle = new TextView(MainActivity.MainContext);
MyTitle.setLayoutParams(
new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
)
);
MyTitle.setText(R.string.mytitle);
((LinearLayout) FragmentView).addView(MyTitle );
}
}
MainActivity.MainContext is stored as public static in the main activity's class and is retrieved when creating the activity by getApplicationContext().
Now, the problem lays here:
FragmentView = inflater.inflate(
R.layout.tasks,
new LinearLayout(MainActivity.MainContext)
);
Because of this, the MyTitle text view is not displayed inside the fragment. If I do this:
FragmentView = inflater.inflate(R.layout.tasks, null);
it is displayed but I get a warning:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
I would like do it properly and not get any warnings and I don't find a way. I also tried:
FragmentView = inflater.inflate(R.layout.tasks, container);
but the application crashes.
You should pass container as a parent.
FragmentView = inflater.inflate(
R.layout.tasks,
container,
false
);
Also you should specify false, so LayoutInflater does not attach the inflated view to parent. The inflated view is attached to parent by FragmentManager.

Access to view elements in fragments

I have an activity which receive data from other activity. This data has to be showed in a TextView in a fragment inflated in the activity. So, the code is:
public class DetailActivity extends Activity {
private int idEstablecimiento;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit(); //Here the fragment is added to the activity
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
idEstablecimiento = extras.getInt("idEstablecimiento");
}
TextView textView = (TextView) this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
textView.setText(this.idEstablecimiento);
}
//...
//Non related stuff...
//...
public static class PlaceholderFragment extends Fragment {
private int idEstablecimiento;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
return rootView;
}
}
The problem is that the textView in the onCreate() method is null, why is this happening if the fragment has been already inflated? Shouldn't I be able to access to the IU elements once the fragment has been inflated in the onCreate() method? Or if not, what is the rigth way to do access to it?
You have to find the TextView in the Fragment.
public static class PlaceholderFragment extends Fragment {
private int idEstablecimiento;
public PlaceholderFragment() {
}
public PlaceholderFragment(int idEstablecimiento) {
this.idEstablecimiento = idEstablecimiento;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
TextView text = rootView.findViewById(R.id.nombreView);
text.setText(idEstablecimiento);
return rootView;
}
And then you can get the text and commit it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Bundle extras = getIntent().getExtras();
if (extras != null) {
idEstablecimiento = extras.getInt("idEstablecimiento");
}
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment(idEstablecimiento)).commit();
}
}
Regards
Nils
If you want to get the View from a fragment layout you should use the view inside the oncreateView of the fragment and then get the textView inside the oncreate view..
example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail,
container, false);
TextView textView = (TextView) rootView.this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
textView.setText("I am here");
return rootView;
}

How to get an id of a View inside a Fragment

Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}

Before onCreateView starts

In PageFragment; I inflate a layout in onCreateView. But I want to inflate this layout before onCreateView loads. It could be inflated one time / or for every fragment; not important.
How can I achieve it ?
public class PageFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page_quiz, container, false);
tv = (TextView) view.findViewById(R.id.Text);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.questionList);
return view;
}
}
You just have to provide a layout as the return parameter in onCreateView(), that does not means you have to inflate it there. Layout can come from anywhere. You can Inflate the layout in onAttach() or in onCreate().
use getActivity() to get a LayoutInflater instance:
View layout = LayoutInflater.from(getActivity()).inflate(........);

Categories

Resources