Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,806,072 Members 47,074 Now Online
XDA Developers Android and Mobile Development Forum

TextView shows wrong text

Tip us?
 
Xintax
Old
#1  
Junior Member - OP
Thanks Meter 2
Posts: 13
Join Date: Jan 2014
Default TextView shows wrong text

Hi,

i'm reading a book about android app development (Learn Android App Development by Wallace Jackson).
But i'm having troubles with my TextViews..
This is what i get when i open the app on the android emulator:



It is supposed to show data like the planet name, mass, gravity,... I tried to locate the mistake in the code, but i never found it..
I want to copy paste the code, but maybe it's easier if i post the entire project

https://www.dropbox.com/sh/235c7k0psaoejxk/I903eizE7A

Could anyone help me find the bug? :/
 
coolbud012
Old
#2  
coolbud012's Avatar
Senior Member
Thanks Meter 24
Posts: 202
Join Date: Sep 2012
Location: Bangalore
I think pasting the activity would be preferred becoz it takes much more time while downloading your code, importing, etc...Also for ppl like me sitting in office without access to particular tools can check the code...

Thanks

Aptitude Cracker - An Application for all Competitive Exams and all In One Aptitude Preparation
---------------------------------------------------------------------------------------------------------------------------------
DroidAcid - Get all android tutorials and Micromax CanvasHD A116 guides.
 
Xintax
Old
#3  
Junior Member - OP
Thanks Meter 2
Posts: 13
Join Date: Jan 2014
Quote:
Originally Posted by coolbud012 View Post
I think pasting the activity would be preferred becoz it takes much more time while downloading your code, importing, etc...Also for ppl like me sitting in office without access to particular tools can check the code...

Thanks
Oh yeah sorry, I didn't think of that.

This is the XML:

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_name_label" />
    
    <TextView 
        android:id="@+id/dataView1" 
        android:layout_toRightOf="@+id/textView1" 
        android:layout_marginLeft="75dp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_name_label" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_mass_label" 
        android:layout_below="@+id/textView1"/>
    
    <TextView
        android:id="@+id/dataView2" 
        android:layout_toRightOf="@+id/textView2" 
        android:layout_alignStart="@+id/dataView1" 
        android:layout_below="@+id/dataView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_mass_label" />
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_gravity_label" 
        android:layout_below="@+id/textView2"/>
        
    <TextView 
        android:id="@+id/dataView3" 
        android:layout_toRightOf="@+id/textView3" 
        android:layout_alignStart="@+id/dataView2" 
        android:layout_below="@+id/dataView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_gravity_label" />
    
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_colonies_label" 
        android:layout_below="@+id/textView3"/>
    
    <TextView android:id="@+id/dataView4" 
        android:layout_toRightOf="@+id/textView4" 
        android:layout_alignStart="@+id/dataView3"
        android:layout_below="@+id/dataView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_colonies_label" />
    
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_population_label" 
        android:layout_below="@+id/textView4"/>
    
    <TextView 
        android:id="@+id/dataView5" 
        android:layout_toRightOf="@+id/textView5" 
        android:layout_alignStart="@+id/dataView4" 
        android:layout_below="@+id/dataView4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_population_label" />
    
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_military_label" 
        android:layout_below="@+id/textView5"/>
        

    <TextView 
        android:id="@+id/dataView6" 
        android:layout_toRightOf="@+id/textView6" 
        android:layout_alignStart="@+id/dataView5" 
        android:layout_below="@+id/dataView5" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_military_label" />
    
    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_bases_label" 
        android:layout_below="@+id/textView6"/>
    
    <TextView 
        android:id="@+id/dataView7" 
        android:layout_toRightOf="@+id/textView7" 
        android:layout_alignStart="@+id/dataView6" 
        android:layout_below="@+id/dataView6" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_bases_label" />
    
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_forcefield_label" 
        android:layout_below="@+id/textView7"/>   
        
    <TextView
        android:id="@+id/dataView8"
        android:layout_toRightOf="@+id/textView8"
        android:layout_alignStart="@+id/dataView7"
        android:layout_below="@+id/dataView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_forcefield_label"/>

</RelativeLayout>
And this is the java:

Code:
package chapter.two.hello.world;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	WorldGen earth = new WorldGen("Earth", 5973, 9.78);		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setStartUpWorldValues();

	}
	protected void setStartUpWorldValues() {
		earth.setPlanetColonies(1);
		earth.setPlanetMilitary(1);
		earth.setColonyImmigration(1000);
		earth.setBaseProtection(100);
		earth.turnForceFieldOn();
		
	}
@SuppressWarnings("unused")
	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));
		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
 
coolbud012
Old
#4  
coolbud012's Avatar
Senior Member
Thanks Meter 24
Posts: 202
Join Date: Sep 2012
Location: Bangalore
Quote:
Originally Posted by Xintax View Post
Oh yeah sorry, I didn't think of that.

This is the XML:

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_name_label" />
    
    <TextView 
        android:id="@+id/dataView1" 
        android:layout_toRightOf="@+id/textView1" 
        android:layout_marginLeft="75dp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_name_label" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_mass_label" 
        android:layout_below="@+id/textView1"/>
    
    <TextView
        android:id="@+id/dataView2" 
        android:layout_toRightOf="@+id/textView2" 
        android:layout_alignStart="@+id/dataView1" 
        android:layout_below="@+id/dataView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_mass_label" />
    
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_gravity_label" 
        android:layout_below="@+id/textView2"/>
        
    <TextView 
        android:id="@+id/dataView3" 
        android:layout_toRightOf="@+id/textView3" 
        android:layout_alignStart="@+id/dataView2" 
        android:layout_below="@+id/dataView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_gravity_label" />
    
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_colonies_label" 
        android:layout_below="@+id/textView3"/>
    
    <TextView android:id="@+id/dataView4" 
        android:layout_toRightOf="@+id/textView4" 
        android:layout_alignStart="@+id/dataView3"
        android:layout_below="@+id/dataView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_colonies_label" />
    
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_population_label" 
        android:layout_below="@+id/textView4"/>
    
    <TextView 
        android:id="@+id/dataView5" 
        android:layout_toRightOf="@+id/textView5" 
        android:layout_alignStart="@+id/dataView4" 
        android:layout_below="@+id/dataView4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_population_label" />
    
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_military_label" 
        android:layout_below="@+id/textView5"/>
        

    <TextView 
        android:id="@+id/dataView6" 
        android:layout_toRightOf="@+id/textView6" 
        android:layout_alignStart="@+id/dataView5" 
        android:layout_below="@+id/dataView5" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_military_label" />
    
    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_bases_label" 
        android:layout_below="@+id/textView6"/>
    
    <TextView 
        android:id="@+id/dataView7" 
        android:layout_toRightOf="@+id/textView7" 
        android:layout_alignStart="@+id/dataView6" 
        android:layout_below="@+id/dataView6" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/planet_bases_label" />
    
    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_forcefield_label" 
        android:layout_below="@+id/textView7"/>   
        
    <TextView
        android:id="@+id/dataView8"
        android:layout_toRightOf="@+id/textView8"
        android:layout_alignStart="@+id/dataView7"
        android:layout_below="@+id/dataView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/planet_forcefield_label"/>

</RelativeLayout>
And this is the java:

Code:
package chapter.two.hello.world;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	WorldGen earth = new WorldGen("Earth", 5973, 9.78);		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setStartUpWorldValues();

	}
	protected void setStartUpWorldValues() {
		earth.setPlanetColonies(1);
		earth.setPlanetMilitary(1);
		earth.setColonyImmigration(1000);
		earth.setBaseProtection(100);
		earth.turnForceFieldOn();
		
	}
@SuppressWarnings("unused")
	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));
		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
Have gone through this but didnt get what exactly you are trying to achieve...Also you have posted the main Activity in which you have created a member of "WorldGen" , whats in this class?

Actually to be frank I didnt get what exactly you are trying to do here...

I would be good if you make an image in "Paint" , rough image, and show what you are exactly trying to achieve?

Thanks

---------- Post added at 09:07 PM ---------- Previous post was at 09:06 PM ----------

If possible upload the project on Github.com

Aptitude Cracker - An Application for all Competitive Exams and all In One Aptitude Preparation
---------------------------------------------------------------------------------------------------------------------------------
DroidAcid - Get all android tutorials and Micromax CanvasHD A116 guides.
 
Xintax
Old
(Last edited by Xintax; 3rd January 2014 at 04:19 PM.)
#5  
Junior Member - OP
Thanks Meter 2
Posts: 13
Join Date: Jan 2014
Well i'm trying to get the text on the left to change into the planet name (earth) planet gravity (9.8),...



I think i can't make it more clear with a paint image with my painting skills

I'm setting up github as we speak, i'll upload it in a sec.

EDIT: Here it is: https://github.com/Xintax/HlloWrld

Thanks!
 
EmptinessFiller
Old
#6  
Member
Thanks Meter 21
Posts: 88
Join Date: Sep 2013
Did you tried hard-coded values?
For ex.: textview1.setText("Earth");
And so on?

Regards
The Following User Says Thank You to EmptinessFiller For This Useful Post: [ Click to Expand ]
 
SimplicityApks
Old
#7  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 323
Join Date: May 2013
Location: Aachen
Just interested, how long does it take to start the activity on devices not running a Snapdragon 800? Seriously, your layout files shouldn't be nearly as long. Try to use a ListView instead! This is heavily optimised and perfect for what I think you are trying to do here.
The Following User Says Thank You to SimplicityApks For This Useful Post: [ Click to Expand ]
 
Xintax
Old
#8  
Junior Member - OP
Thanks Meter 2
Posts: 13
Join Date: Jan 2014
Quote:
Originally Posted by EmptinessFiller View Post
Did you tried hard-coded values?
For ex.: textview1.setText("Earth");
And so on?

Regards
Quote:
Originally Posted by SimplicityApks View Post
Just interested, how long does it take to start the activity on devices not running a Snapdragon 800? Seriously, your layout files shouldn't be nearly as long. Try to use a ListView instead! This is heavily optimised and perfect for what I think you are trying to do here.
Well i tried to hard-code the values, and that works.. But my idea is that users can change the planet, and can make their own, so i can't hard-code the values because then they can't change :/

I'll try if the listview works, it does take a long time

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Learn How to Make a Live Wallpaper all by Yourself

Stillimages can bebeautiful, but some of you prefer to see animations on your screens. … more

Google Play Services Updated to Version 6.1

In addition to Android operating system proper, Google is focusing hard on giving the developers … more

CyanogenMod 11 M10 Available for Supported Devices

Summer vacation isover for most students out there, and it’s the time to get back to … more

XDA Forums Added for the First Batch of Android One Devices!

Just yesterday, we talked about the highly anticipated launch of the first batch … more