I run a setText command inside of a fragment activity to try and set the text of a textView in the parent activity, but it's not working. Any ideas?
TextView text = (TextView) getView().findViewById(R.id.status);
text.setText("Text from a fragment");
I don't get an error in eclipse, but I get a null pointer exception during runtime. Of course it happens at the line where I setText. Any ideas on how to do this?
Use getActivity(). It will cause the findViewById() method to start the search at the base activity and bubble up until it finds your "r.id.status".
TextView text = (TextView) getActivity().findViewById(R.id.status);
text.setText("Text from a fragment");
Yes your NPE will probably be because R.id.status is probably not defined in the fragment's view.
getView() will return the view that is generated in your Fragment's onCreateView() method.
Are you trying to set a TextView in your Activity from a (secondary) FragmentActivity or trying to set a TextView in your FragmentActivity from a Fragment?
If it's the 1st option, you'll want to do something like use a message handler and pass the 2nd Activity a message. I don't think this is what you're asking though.
If you're wanting to set a TextView from a Fragment, the general way to do that is to define an interface on your FragmentActivity with a method (updateText() for example) and get the Fragment to call the method. The Activity then handles the TextView update, which works nicely because it can call getView() which will return the view you're looking for. It's similar to my answer posted here
Related
I am trying to update a TextView by using findViewByID(), but in my onCreate I am calling a layout which does not include the TextView I want to update-they are seperate files.
I have read through the Android Developer Documentation for findViewByID() but I have not found how to find an ID that is not in the same onCreate layout (which means I cannot use R.id.callable_textview).
How can you make findViewById() find the TextView in a seperate layout not called on the onCreate?
For passing information from Activity A to Activity B, you can use Intent extras. If you need to pass information from Activity B back to Activity A, you can use startActivityForResult().
You shouldn't be trying to access the Views of one Activity from another Activity. That's not how it works.
I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?
I have been trying for days to find a solution to my problem, but now I've decided to try to ask you. I'm a noob for programming Android, so please forgive me.
I have a main Activity with a Listview in it.
I am using a Simplecursoradapter to feed it with information from my database and a customized layout I made as an XML.
my problem is, I want to change some of the terms or the units that I used in this custom layout, which is not feed through the database.
But if I use settext to the TextViews in there it wont run, the app will crash, because my setContentView is set to another layout for this class, I guess. I have been looking at inflaters, trying to see if I could change the XML programmable, use a string from String.xml and change that. But as far as I can see these are not an option. Later I found this code on StackOverflow
Activity activity = (Activity)getContext();
TextView t = (TextView)activity.findViewById(R.id.txtDisponible);
t.setText("E-ticket validado");
But I can't get this to work because the first line isn't working for me. The Activity won't give me the getcontext method. And even if I did get this to work, I wouldn't know if this would work. Could you guide me in which direction I need to go? As of now, I don't have a class for my ListView, is that the way to go. I want to keep this as simple as I can.
Thank you for your help.
EDIT 1:
Okay i tried to put this in my code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_list, null);
TextView t = (TextView) view.findViewById(R.id.kmListview);
t.setText("bok");
The app doesn't crash, but it doesn't change the textview either.
Activity extends ContextWrapper class and you should use this or getBaseContext() because there is no method like getContext() which extends ContextWrapper class.
So use this as Context or getBaseContext() in your Actitiy and I think your problem will be resolved.
Basically I am trying to create an app that passes data filled in an EditText on on one fragment, into a TextView on another fragment on a button click(The buttons is in the first fragment with the EditText). I use the SlidingTabLayout. I have 2 java classes that both extend Fragment and both inflate separate xml layouts(in the onCreateView). I have a java MainActivty with a public class"SectionsPagerAdapter that extends FragmentPagerAdapter, which depending on the swipe of the user displays 1 of the 2 Fragment classes. I am really confused on how I can send data between the 2 fragment sot that from the EditText in 1 fragment can get sent to the TextView in the other fragment on a button click. Could the suggested solutions be explained as simple as possible because I am relatively new to Android Studio. Many thanks in advance.
As per my understanding, basically you want to pass data between two fragments.
You can use activity for that from where fragments are initialized.
you can do this.
in MainActivity.java:
have a function setData(Object obj) and getData() which returns that object.
From fragment:
You can call those function of activity to save your data and get your data.
Here's way:
MainActivity activity = (MainActivity) getActivity();
Object obj = activity.getData();
activity.setData(obj);
I hope it helps.
I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.