In the XML file for my layout I added a fragment.
The layout for my fragment is a LinearLayout and it contains a text view.
I want to get a reference to the LinearLayout object in my fragment class, but despite trying everything I can think of or find online, it is always null.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
text = (TextView) getActivity().findViewById(R.id.text);
layout = (LinearLayout) getActivity().findViewById(R.id.layout);
if(layout == null){
text.setText("null");
}
}
This code is in my fragment class and results in the TextView's text being set to "null" since the layout is null. How could this be since I am trying to get the references to the layout and the textview in the exact same way?
onCreate in activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
}
Method 2
I tried getting a reference to the LinearLayout in my activity. Here is the activity's onCreate with this attempt.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
manager = getFragmentManager();
MyFragment = (MyFragment) manager.findFragmentById(R.id.fragment);
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
TextView tv = (TextView) findViewById(R.id.text);
if(ll == null){
tv.setText("null");
}
}
This also changed the text to "null."
The problem is fixed in both cases by saying layout = (LinearLayout) getView() in the fragment class or layout = (LinearLayout) fragment.getView() in the activity. This doesn't answer why what I was doing didn't work (which could be helpful) but this solves the problem.
Related
I want to update TextView in onCreate(), so that text should be set when Activity Interface appears, what I've tried is:
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sem_selection);
TextView T =(TextView)findViewById(R.id.textView1);
T.setText("required text");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
but i get error when activity starts , as soon as i comment this line :
T.setText("required text");
App runs fine without error. How should I do this? Is there any other method?
You are referencing TextView T from fragment you will get null because in your activity you are using activity_sem_selection and getting TextView T from activity_sem_selection layout.
So use T in onCreateView method of fragment.
For example
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment__sem_selection, container,
false);
TextView T =(TextView)rootView.findViewById(R.id.textView1);
T.setText("required text");
return rootView;
}
Note : change fragment__sem_selection with your fragment layout.
Remove
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Check textView1 is present in activity_sem_selection layout.Otherwise you will get NPE.Try to place a TextView with id textView1 in activity_sem_selection layout. Otherwise you can remove those assignment and referencing for that TextView T and place those inside fragment's onCreateView(...).
I am learning to develop apps for android from a book and after following the instructions I end up with the following.
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
I get wavy red underline under the dataView1 in findViewById(). Eclipse informs that dataView1 cannot be resolved or is not a field.
The complete code look like this and i get wavy red underline under all dataView. It says nothing about this error in the book.
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
Here the MainActivity
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setStartUpWorldValue();
setStartUpScreenText();
}
protected void setStartUpWorldValue(){
earth.setPlanetColonies(1);
earth.setPlanetMilitary(1);
earth.setColonyImmigration(1000);
earth.setBaseProtection(100);
earth.turnForceFieldOn();
}
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
It seems you built your views inside fragment_main.xml and not activity_main.xml.
When you first create a new android project, you have these files which are automatically created and opened:
Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml file. Finally, you tried to do a basically event with this view inside your Activity, something like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Using layout activity_main.xml
// You try to set a simple text on the view (TextView) previously added
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Simple Text"); // And you get an error here!
/*
* You do an fragment transaction to add PlaceholderFragment Fragment
* on screen - this below snippnet is automatically created.
*/
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..
Solution: Move all your stuff inside onCreateView method into the Fragment class.
Call views and do something in the related fragment and not the parent activity.
Or you can do all inside the activity, by removing these lines which to not add a fragment on your UI:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
This above create and add a fragment "above" your activity layout. And then, setting the right layout here:
setContentView(R.layout.fragment_main);
My custom FragmentSettings() extends android.preference.PreferenceFragment and I simply add it in my activity.
I want to inflate (add) as well the layout.logo which has gravity:bottom. However with my code the inflated layout.logo is not visible. I guess because I use inflate() with getFragmentManager()? How can I correctly add a layout in this case?
public class ActivitySettings extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentSettings details = new FragmentSettings();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
getLayoutInflater().inflate(R.layout.logo, null );
}
}
PS: R.layout.logo is an ImageView.
Try this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentSettings details = new FragmentSettings();
details.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
LayoutInflater inflater = LayoutInflater.from(context);
View view =inflater.inflate(R.layout.logo, null);
// may be logo is a layout file, if not then put your layout file which contain an ImageView
ImageView imageView = (ImageView)view.findViewById(R.id.imageView1);
}
but you said that R.layout.logo in an ImageView . you know, ImageView has an id and you will find it from R.id.(your ImageView's id ) hear. just i assume that imageView1 is and id of your ImageView.
I´ve been looking for answers, and have tried a lot of things, but nothing is working...
So, Im doing what I think is a simple app (I just started learning)
So I have an activity sending a couple of string to another where a couple of TextViews should chnage ther values to the new String, but this is not happening. Here you have the code:
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Intent intent = getIntent();
String mail = intent.getStringExtra(MainActivity.MAIL);
String password = intent.getStringExtra(MainActivity.PASSWORD);
setContentView(R.layout.fragment_login);
TextView displayMailSetter = (TextView) findViewById(R.id.mailView);
displayMailSetter.setText(mail);
displayMailSetter.postInvalidate();
TextView displayPasswordSetter = (TextView)findViewById(R.id.passwordView);
displayPasswordSetter.setText(password);
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
}
}
Try taking out these lines of code:
displayMailSetter.postInvalidate();
// Don't take out your displayPasswordSetter initilization and setText
displayPasswordSetter.postInvalidate();
displayMailSetter.postInvalidate();
setContentView(R.layout.activity_login);
Change these lines of code:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
to
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_login);
and take out the origional (right under the intents).
setContentView(R.layout.fragment_login);
The problem was that you were setting the layout to 3 different layouts. The first and third were the same layout, but didn't have the TextViews. The first and third are unnessecary, you should only set the layout once. Once you set it to fragment_login, the layout had the TextViews, set the text of them, but the layout was then changed to a layout without the TextViews.
The issue I'm having is that I don't know how to get a pointer to a layout within a fragment. It's obvious that to get a layout pointer in Java you would do something like this:
LinearLayout llTemp = (LinearLayout) findViewById(R.id.llTemp)
Something along those lines.
Now what I'm doing is grabbing information from a server in the main class and load a fragment within the same class. I would like to populate the fragment with the information loaded from the outer class. Is there any way to do this? I would have just grabbed the layout from within the fragment and do it that way but I cannot make a reference to it as it's in the fragment.
I'm sure this is a common problem but I couldn't find anything on it specifically like this.
Thanks in advance,
Cheers,
Jake
To Answer the comment:
View view = inflater.inflate(R.layout.main_frag, container, false);
mainLayout = (LinearLayout) view.findViewById(R.id.ll_MainFrag);
return view;
This is what is in my onCreateView.
Okay, just to add how I'm instantiating the Fragment:
private int MAIN = 1;
FragmentManager fm = getSupportFragmentManager();
fragments[MAIN] = new MainFragment();
FragmentTransaction transaction = fm.beginTransaction();
transaction.commit();
getSupportFragmentManager().beginTransaction().add(R.id.flMain, fragments[MAIN]).commit();
From here I would like to be able to do something like:
fragments[MAIN].createTextView();
When creating a Fragment, create public methods to set data:
public class MyFragment extends Fragment {
private TextView text1;
private TextView text2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = LayoutInflater.from(getActivity()).inflate(R.layout.simple_list_item_2,container,false);
text1 = (TextView) layout.findViewById(R.id.text1);
text2 = (TextView) layout.findViewById(R.id.text2);
return super.onCreateView(inflater, container, savedInstanceState);
}
public void setData(String t1, String t2){
text1.setText(t1);
text2.setText(t2);
}
}
When adding a fragment in parent activity, give it a unique tag:
MyFragment f = new MyFragment();
getFragmentManager().beginTransaction().add(f,"my_fragment").commit();
Later, you can search the fragment from parent activity and call some methods on it:
MyFragment frg = (MyFragment) getFragmentManager().findFragmentByTag("my_fragment");
if(frg != null){
frg.setData("abc","def");
}
Also, if fragment was added from a layout, you can find the fragment by its id.