Error in dataView - android

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);

Related

How to implement two layout for different screen size in MainActivity

I am following Udacity's course where at last during completion I met with this error.
You can also check the Udacity's code by clicking here
and my layout files 1 & 2.
**
Thanks in advance.
**
com.thelazyprogrammer.harshit.mycustomapps.ui.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:6998)
This error is pointing to setContentView(R.layout.activity_main);
But don't know what is missing.
My code:-
public class MainActivity extends AppCompatActivity implements MasterListFragment.OnImageClickListener
{
// Variable to store the values of list index of selected image.
private int headIndex,
bodyIndex,
legIndex;
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/*<---THE ABOVE ERROR POINTING BELOW LINE'S CODE--->*/
setContentView(R.layout.activity_main);
/*<------------------------------------------------>*/
/*Intent intent = new Intent(this, AndroidMeActivity.class);
startActivity(intent);*/
if(findViewById(R.id.android_me_linear_layout) != null)
{
mTwoPane=true;
// Setting grid view into two columns.
GridView gridView = (GridView) findViewById(R.id.images_grid_view);
gridView.setNumColumns(2);
// Getting rid of the button in the tablet mode.
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setVisibility(View.GONE);
if(savedInstanceState!=null)
{
FragmentManager fragmentManager = getSupportFragmentManager();
BodyClassFragment headFragment, bodyFragment, legFragment;
// Creating new head, body, leg.
headFragment = new BodyClassFragment();
headFragment.setImageIds(AndroidImageAssets.getHeads());
bodyFragment = new BodyClassFragment();
bodyFragment.setImageIds(AndroidImageAssets.getBodies());
legFragment = new BodyClassFragment();
legFragment.setImageIds(AndroidImageAssets.getLegs());
// Adding fragment to it's container.
fragmentManager.beginTransaction()
.add(R.id.head_container, headFragment)
.add(R.id.body_container, bodyFragment)
.add(R.id.leg_container, legFragment)
.commit();
}
}
else
{
mTwoPane=false;
}
}
I think you have to learn the basic of display size from Google Docs.

Get reference to LinearLayout in fragment

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.

Updating TextView in onCreate()

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(...).

Inflate layout in an activity with PreferenceFragment

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.

Trying to change TextView String

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.

Categories

Resources