I have a Fragment Activity i need to set Text to the text view in that Activity,when i am using following code getting null pointer exception.
can any other approach is there to set data for text view .
public class Introduction extends Fragment {
TextView txt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.activity_introduction, container, false);
txt=(TextView) v.findViewById(R.id.officeheader);
return v;
}
public void setText(String text){
txt.setText(text);
}
}
and in My Fragment Activity class :
frgManager=getSupportFragmentManager();
frgManager.beginTransaction()
. replace(R.id.fragmentoffice, introduction,"introduction")
.commit();
introduction.setText(" Office Coffee that will \n have everyone talking ");
set string in bundle
Bundle bundl = new Bundle();
bundl.putString("Text", text);
introduction.setArguments(bundl)
frgManager=getSupportFragmentManager();
frgManager.beginTransaction()
. replace(R.id.fragmentoffice, introduction,"introduction")
.commit();
get text from bundle
public class Introduction extends Fragment {
TextView txt;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
String str = getArguments().getString("Text");
View v= inflater.inflate(R.layout.activity_introduction, container, false);
txt=(TextView) v.findViewById(R.id.officeheader);
setText(str);
return v;
}
public void setText(String text){
txt.setText(text);
}
}
you can write introduction constructor ,which contain a parameter Type String ,
and on the onCreateView ,you can initialize the textivew and settext
Related
i was working with a NavigationDrawer for a menu and there is a fragment for every item on the menu for example i have a home menu which have a homefragemnt.xml and a homefragment.java so in the xml file file there is a textview i wanted to change that textviews values using the MainActivity.java
and used the following code:TextView t1 = (TextView) findViewById(R.id.changetxt);t1.setText("ChangedText");
changetxt is the id of the TextView in the homefragment.xml file
but this code give a runtime error saying Attempt to invoke virtual method on a null object reference.
What is causing this problem and how do i solve it?
You will have to get the reference of textview from the fragment view not activity.
The view which you inflated in the onCreateView of fragment.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.myview,container,false);
TextView t1 = (TextView) view.findViewById(R.id.changetxt);t1.setText("ChangedText");
return view;
}
here view is the View that the fragment is initaialised with.
this link will help you
Fragment :
public class MyFragment extends Fragment {
TextView t1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
t1 = (TextView) view.findViewById(R.id.changetxt);
return view;
}
public void setTextViewText(String value){
t1.setText(value);
}
}
Activity :
myFragment.setTextView("ChangedText");
Hope it helps you.
I'm doing a Navigation Drawer app. I have all the cases working, I mean, when in touch a section the app goes to the right View. But when I want to start coding I don't know how to use methods inside the View. I think my code will explain better than me:
public class News extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setText();
return inflater.inflate(R.layout.lay_news, container, false);
}
public void setText(){
TextView texts = (TextView) getView().findViewById(R.id.textView);
texto.setText("hi");
}
}
NullPointerException when I call the method setText. I know that it's the TextView definition, but I don't know how to "find" it in the layout without getView() or getActivty(), both return a NullPointerException when the view loads.
Just do
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.lay_news, container, false);
setText(v);
return v;
}
and
public void setText(View v){
TextView texts = (TextView)v.findViewById(R.id.textView);
texto.setText("hi");
}
I have a fragment with a control (a TextView) with id R.id.currentCycleIndicator and want to programmatically set its value from within the fragment.
Both
getActivity().findViewById(R.id.currentCycleIndicator) and
getView().findViewById(R.id.currentCycleIndicator)
return null.
How can I get reference to that TextView inside the fragment code?
Try getting its value in onCreateView() callBack function of your Fragment like below :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
TextView textView = (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}
You can do that by overriding the onViewCreated function as below :
private TextView tv;
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tv = (TextView) view.findViewById(R.id.currentCycleIndicator);
}
Inflate your view inside onCreateView and refer to your TextView from there.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layoutwheretextviewis, container, false);
TextView text= (TextView) view.findViewById(R.id.currentCycleIndicator);
return view;
}
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;
}
I am trying to implement Fragments in my Project using the https://github.com/JakeWharton/Android-ViewPagerIndicator plugin.
My Fragment
public final class NearByFragment extends Fragment {
private static String TAG = "bMobile";
public static NearByFragment newInstance(String content) {
NearByFragment fragment = new NearByFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_search_nearby, null);
}
}
Now I want to execute some code like start start a new thread and download a JSON from the server and then assign a list view from the content I download. In fragments we are assigning views in onCreateView. so where should I write the
listview = (ListView) findViewById(R.id.list_items);
((TextView) findViewById(R.id.title_text))
.setText(R.string.description_blocked);
and other code to generate the Fragment view ?
You can search within the view that you just inflated:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_search_nearby, null);
listview = (ListView)v.findViewById(R.id.list_items);
((TextView)v.findViewById(R.id.title_text))
.setText(R.string.description_blocked);
return v;
}
You can also use the Fragment.getView() function elsewhere in your code and call that view's findViewById() member.
You can use getActivity().findByView() from the Fragment if you want to have access to the layout elements. Alternatively, you can just put the findByView() call in the main Activity.