NullPointerException while adding view in a fragment dynamically - android

I am trying to dynamically add a TableLayout into a LinearLayout inside a fragment.
Code below:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootview = inflater.inflate(R.layout.dashboard_fragment, container, false);
dash_board = (LinearLayout) rootview.findViewById(R.layout.dashboard_fragment);
table = new TableLayout(getActivity());
TableRow tr_header = new TableRow(getActivity());
TextView Name = new TextView(getActivity());
Name.setText("Name");
LayoutParams Name_params = new LayoutParams();
Name_params.width= 100;
Name.setLayoutParams(Name_params);
Name.setPadding(2, 0, 2, 2);
tr_header.addView(Name);
TextView Points = new TextView(getActivity());
Points.setText("Points");
LayoutParams point_params = new LayoutParams();
point_params.width= 100;
Points.setLayoutParams(point_params);
Points.setPadding(2, 0, 2, 2);
tr_header.addView(Points);
table.addView(tr_header);
dash_board.addView(table);
return rootview;
}
But while adding table to the dash board layout (dash_board.addView(table);) I am getting a NullPointerException.
What could the issue be?

Actually there is no dashboard_fragment as a ViewGroup in rootView because the rootView is exacly the dashboard_fragment as a ViewGroup.So these two lines of code are the cause of exception:
View rootview =
inflater.inflate(R.layout.dashboard_fragment,container, false);
dash_board =
(LinearLayout)rootview.findViewById(R.layout.dashboard_fragment);
Actually the logic will be correct if you do like this:
View rootview =
inflater.inflate(R.layout.dashboard_fragment,container, false);
dash_board =
(LinearLayout) rootView;
And this is the simplest form for this process:
dash_board
=(LinearLayout)inflater.inflate(R.layout.dashboard_fragment,container, false);

Instead of this
View rootview = inflater.inflate(R.layout.dashboard_fragment, container, false);
dash_board = (LinearLayout) rootview.findViewById(R.layout.dashboard_fragment);
it should be
LinearLayout dash_board = (LinearLayout)inflater.inflate(R.layout.dashboard_fragment, container, false);

Related

Can we modify a layout (programmatically) before inflating it in a fragment?

I have a simple fragment:
public class FR_FragmentPopup extends Fragment {
public FR_FragmentPopup() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivity.getMain().doFragment(new FR_RecycleViewSettings2(),R.id.over_frame_bottom,false);
return inflater.inflate(R.layout.fragment_popup, container, false);
}
}
I want to modify the xml to wrap height, but programmatically because I'm using the layout in other fragments also.I know that R.layout.fragment_popup is an int and not a view which makes it difficult to use edit it.
return inflater.inflate(
findViewById(R.layout.fragment_popup).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
, container, false);
How can I do this?
You may do as following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_help, container, false);
rootView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return rootView;
}
Theoretically:
View view = inflater.inflate(R.layout.fragment_popup, container, false);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return view;
Practically:
Never tried.

Add buttons dynamicaly in fragment

want to add buttons dynamically. I tried like this:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.home, container, false);
Button newText = new Button(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
return view;
}
Inside your View you can add a layout then add your button inside the layout,
like so:
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layoutContainer);
layout.addView(newText);

Update textview in fragment

This might be a very simple problem, but I can't seem to get it to work.
I'm trying to update a textview in a Fragment on "OnCreateView"
My code looks like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_display_product, container, false);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return inflater.inflate(R.layout.fragment_display_product, container,false);
}
The text in textview txtAge never get set and I can't seem to find a reason.
I'm new to android so sorry if this is a stupid question.
/Birger
Do it like this-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_display_product, null);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return v;
}
Return the same view which you have inflated already.

Adding dynamic views in a fragment

I am trying to add a dynamic view for my fragment.
I am using this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout myLayout = new RelativeLayout(Builtprofile.context);
myLayout.setBackgroundColor(Color.BLUE);
RelativeLayout.LayoutParams buttonParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
myLayout.addView(myButton, buttonParams);
View rootView = inflater.inflate(R.layout.q1, container, false);
// I want to add myLayout in place of R.layout.q1
return rootView;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
Button myButton = new Button(Builtprofile.context);
myButton.setText("Press me");
myButton.setBackgroundColor(Color.YELLOW);
RelativeLayout.LayoutParams buttonParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
View rootView = inflater.inflate(R.layout.q1, container, false);
RelativeLayout myLayout = (RelativeLayout)rootView.findViewById(R.id.mainLayout);
myLayout.setBackgroundColor(Color.BLUE);
myLayout.addView(myButton, buttonParams);
return rootView;
}
Have an empty xml layout with just a RelativeLayout called "mainLayout"(or what ever you want to call it). That way you can append any dynamically generated controls
LayoutInflater vi = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.xml, null);
container.addView(v);

What to do in the Fragment onCreateView Method?

What I have are 3 Layouts.
fragment_tag, fragment_stunde and fragment_fach
fach should be in stunde and stunde in tag.
And in fach are some TextViews I want to set the text.
So my code so far is:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
database = new Database(context);
database.open();
for (int j = 1; j < 12; j++) {
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
String[][] vplanDaten = database.getVplan(Integer.valueOf(DateHelper.get(DateHelper.WEEK_OF_YEAR)), index + 1, j);
for (int k = 0; k < vplanDaten.length; k++) {
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
TextView fach_lehrer = (TextView) fach.findViewById(R.id.textView_lehrer);
TextView fach_fach = (TextView) fach.findViewById(R.id.textView_fach);
TextView fach_raum = (TextView) fach.findViewById(R.id.textView_raum);
fach_lehrer.setText(vplanDaten[k][0]);
fach_fach.setText(vplanDaten[k][1]);
fach_raum.setText(vplanDaten[k][2]);
}
}
database.close();
return tag;
}
But this gives me an Error:
java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)
So how do I have to change the code that that what I want to do happens?
Try making the following changes:
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
to:
LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);
Similarly:
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
to:
LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);
And:
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
to:
LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);
Change the LinearLayout to a View like such:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tag = inflater.inflate(R.layout.fragment_tag, container, true);
//Everything else...
return tag;
}
Also I believe you can only inflate one view per onCreateView otherwise the layouts will replace one another. You need to have three separate fragments that each inflate its own view and call these fragments individually from the top parent activity.

Categories

Resources