Here is the screenshot of the code, I am new to programming and this app I am making is my school project, no experience working with Android studio before( a bit of Java but not too much)
http://prntscr.com/i465km
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
So my question is, why is this section_format error and app is giving me two answers how to fix it: create string value or create field with the same name(section_format)?
setText() method displays the string inside it. Like this
textView.setText("Sample Text");
if you are displaying a message from a method it should return a string value
Like this
private string getString(param1 , param2){
//do something with parameters
return string;
}
Now you can get the message from the method like this:
textView.setText(getString(param1, param2))
Related
I am using fragments in my dynamic application, where the User.java file contains the values and TabbedActivity.java file contains three fragments. I want to set the text to TextView in ProfileFrgament.java. So, I created a TextView in fragment_profile.xml and referenced it from TabbedActivity.java file with the following code
name = findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(this).getUser();
//setting values to textviews
name.setText(user.getUsername());
It does not shows any compilation error, but after opening TabbedActivity.java, the app stops with NullPointerException at line name.setText(user.getUsername()); How to solve this issue?
This is the code of ProfileFragment.java file
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
name = getActivity().findViewById(R.id.name);
//getting the current user
User user = SharedPrefManager.getInstance(getActivity()).getUser();
//setting values to textviews
name.setText(user.getUsername());
return inflater.inflate(R.layout.fragment_profile, container, false);
}
Your TextView is in fragment_profile.xml and you trying to find that TextView in TabbedActivity.java, so NullPointerException is occured
Put this code in ProfileFrgament.java
Change your code with this one
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
name = (TextView) rootView.findViewById(R.id.name);
User user = SharedPrefManager.getInstance(getActivity()).getUser();
name.setText(user.getUsername());
return rootView;
}
You need to do this in the fragment's java file than in the activity's java file.
Now for finding view in fragment.
Replace this code :
name = findViewById(R.id.name);
to :
name = getActivity().findViewById(R.id.name);
Rest of the code can be written as it is already.
Also put this type of operation in onViewCreated method. Putting this code in onCreateView may crash the app.
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.
I created a new project (Tabbed Activity).
This is default code for onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
but then I made some changes to the code:
.
.
textView.setText(getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)));
Toast.makeText(getContext(), getString(R.string.section_format,getArguments().getInt(ARG_SECTION_NUMBER)), Toast.LENGTH_SHORT).show();
return rootView;
.
.
But the Toast value is different from what textview displayed. So the question is, how can I get the exact(accurate) value of section number?
Edit 1: Toast displayed two times when activity first started.
Edit 2: Screenshot added.
AFAIK,text from TextView might change faster. Whereas, the toast will continue showing until its time is up. It take approximately about 5s for the toast to change. So, you might think the section is not the same.
My suggestion is to test using two TextViews.
Hope this helps :)
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;
}
Seems like a simple question to me, but I can't figure it out. I have this code inside my Fragment:
myTextView.setText("Test");
However, this code brakes my app and I am unsure why. Perhaps this is a conflict with Views, but I still don't understand why it wont work. Any suggestions?
How did you set the content?
Edited my answer :) try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.xmlName,
container, false);
TextView tv= (TextView) **view**.findViewById(R.id.textViewName);
tv.setText("yourText");
return view;
}
here is a tutorial for fragments : fragments tutorial