I´m getting stucked on a - probably - "easy" problem...
I´ve got a fragment which displays a TextView.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_rezepte_test, container, false);
TextView responseTV = (TextView) inflatedView.findViewById(R.id.tv_response);
return inflatedView;
}
Next I´ve got a private void GET-Method, whose response body I want to display in this
String responseBody = response.body().string();
responseTV.setText(responseBody);
Everytime the Fragment will open, the app crashes caused by a NPE
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Any idea? Thanks in advance
Let's try to call super.onCreateView(inflater, container, savedInstanceState)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState)
View inflatedView = inflater.inflate(R.layout.fragment_rezepte_test, container, false);
TextView responseTV = (TextView) inflatedView.findViewById(R.id.tv_response);
return inflatedView;
}
Did you check your xml layout (fragment_rezepte_test.xml)? Make sure you have a TextView with the same id.
I am not very clear about the question and also not clear that exactly when you are trying to call this method response.body(), but please check whether your responsebody string is null or not and also use method isAdded() in case of fragments in every important method.
Edit : try to initialize that text view inside onActivityCreated and then use it.
Related
I have 2 fragments each fragments has a different java logic. Where should i put the java logic? if i did put it in the fragments,then error message displays :
cannot resolve method findViewById, Cannont resolve method getApplicationContext
it appear that you did not inflate the view correctly, change your code to be something like this :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.your_fragment, container, false);
mButton = (Button) view.findViewById(R.id.mButton );
list_content = (ListView) view.findViewById(R.id.list_content);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
return view;
}
and you should use 'getActivity()' method for context not 'This' .
You can find other view's Id's in your fragment like this.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout,container,false);
tabLayout = (TabLayout)view.findViewById(R.id.tabs);
return view;
}
When you inflate the view on the oncreateview make a view field like this :
View view = inflater.inflate(R.layout..., container, false);
and return with this view.
After that with this variable you can reach the findViewById, like
view.findViewById
Instead of getApplicationContext you can use getContext in fragment.
Good luck.
I want to set text of a TextView that is inside a layout file,
I tried setContentView() but it isnt working since i am using fragments.
I tried using getResources().getLayout(R.layout.abc);
It returns null
I tried setContentView() but it isnt working since i am using
fragments
That's wrong. You have to override onCreateView and inflate and return the layout you want to show, and you can use onCreateView, and use its first parameter, View view, to call findViewById and access the widgets in your layout. You can read more here and here
inside fragment you can set view inside function onCreateView(), use below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
View view = (LinearLayout) inflater.inflate(R.layout.my_layout, container, false);
return view;
}
here my_layout should be the name of layout file,
now you can get view of it inside function onviewcreated()
public void onViewCreated(View view, Bundle savedInstanceState){
// here you can get your textview and set its value
}
thumbs up, if you find my answer correct
You can inflate your layout like this:
ViewGroup group = LayouInflate.from(context).inflate(R.layout.abc,null);
TextView tv = group.findViewById(R.id.xxx);
you have to inflate the layout through the onCreateView method and then return the View.
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
TextView yourTextView = (TextView)rootView.findViewById(R.id.yourTextViewId);
return rootView;
}
Here's my code in the main activity don'tn know why error in findViewById occurs
.. this is just my first time in JSON paarsing .
public class HomeFragment extends Fragment{
public HomeFragment(){}
View rootView;
ListView list;
ActorsAdapter adapt;
ArrayList<Actors> actorslist;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container, false);
list = (ListView)findViewById(R.id.listFeeds);
actorslist = new ArrayList<Actors>();
return rootView;
new ActorsAsynTask().execute("http://jcasim.5gbfree.com/Project/login.php");
}
listFeeds It's a component I'm pretty sure is a component that lives in your fragment. Since you haven't return the root view yet, the findViewById is not going to find it. you have to wait until it's on screen to ask for it using findViewById (in onStart() method) or ask it form the root view before you return it.
it would be list = (ListView)rootView.findViewById(R.id.listFeeds);
The View of the Fragment is not set before the onCreateView() returns. Therefore the findViewById() tries to find the specified R.id.listFeedsfrom a null reference.
Try this instead list = (ListView)rootView.findViewById(R.id.listFeeds);
I am new to this android application development. I am trying to find textfield using findViewById Eclipse keep asking me to create a new method for findViewById. Here is my code.
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle SavedInstanceState) {
return inflater.inflate(R.layout.myFragment, container,false);
//find textarea
TextView textArea = (TextView) findViewById(R.id.txtField_curDateTime);
}
Please note i have myFragment.java and myFragment.xml files.
Appreciate your help.
Thanks
Do it like this:
View v = inflater.inflate(R.layout.myFragment, container,false);
TextView textArea = (TextView) v.findViewById(R.id.txtField_curDateTime);
return v;
Moreover, define TextView textArea outside the onCreateView so that its available to other methods too.
Try this:
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle SavedInstanceState) {
View vObject = inflater.inflate(R.layout.myFragment, container,false);
//find textarea
TextView textArea = (TextView)vObject.findViewById(R.id.txtField_curDateTime);
return vObject;
}
I am in trouble when trying to get the baseView(inflated in onCreateView and returned) in fragment, because I receive unexpected NoSaveStateFrameLayout.
I don't know why, can anyone explain?
Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtil.d(TAG, "onCreateView ...");
View v = inflater.inflate(R.layout.gen_blank, container, false);
return v;
}
When I run this code in all life cycles:
LogUtil.v(TAG, "getview = "+getView());
It logs the same msg: getview = android.support.v4.app.NoSaveStateFrameLayout#41afdb70
The support library inserts an additional view into the view hierarchy. According to a comment in NoSaveStateFrameLayout.java: "Pre-Honeycomb versions of the platform don't have View.setSaveFromParentEnabled(), so instead we insert this between the view and its parent."
So you will need to either check for this with something like:
View myView = (NoSaveStateFrameLayout)getView();
if (myView != null) {
myView = myView.getChildAt(0);
}
Or keep track of the view returned by onCreateView in an instance variable.