My TextView.setText()
isn't working.
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);//Create a World Object
@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();
}
setStartUpWorldValues();
setStartUpScreenText();//ADDED
}
protected void setStartUpWorldValues(){
earth.setPlanetColonies(1); // Set Planet Colonies to One
earth.setPlanetMilitary(1); // Set Planet Military Bases to One
earth.setColonyImmigration(1000); // Set Planet Population to 1000
earth.setBaseProtection(100); // Set Planet Armed Forces to 100
earth.turnForceFieldOn(); // Turn On the Planet Force Field
}
public void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(earth.planetMass);
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(earth.planetColonies);
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(earth.planetMilitary);
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(earth.planetBases);
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
//setContentView(R.layout.activity_main); DELETED
} //etc
/So my setText()
method doesn't change the default value that I have in the XML file.
Should I put the content of setStartUpScreenText()
method in the onCreate
method? Can it work outside of it?/
Now the application close after running.
LogCat :
06-16 14:59:40.692: W/ResourceType(2231): No package identifier when getting value for resource number 0x00001755
06-16 14:59:40.702: D/AndroidRuntime(2231): Shutting down VM
06-16 14:59:40.702: W/dalvikvm(2231): threadid=1: thread exiting with uncaught exception (group=0xb3a7fba8)
06-16 14:59:40.772: E/AndroidRuntime(2231): FATAL EXCEPTION: main
06-16 14:59:40.772: E/AndroidRuntime(2231): Process: chapter.two.hello_world, PID: 2231
06-16 14:59:40.772: E/AndroidRuntime(2231): java.lang.RuntimeException: Unable to start activity ComponentInfo{chapter.two.hello_world/chapter.two.hello_world.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1755
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.os.Looper.loop(Looper.java:136)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 14:59:40.772: E/AndroidRuntime(2231): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 14:59:40.772: E/AndroidRuntime(2231): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 14:59:40.772: E/AndroidRuntime(2231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 14:59:40.772: E/AndroidRuntime(2231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 14:59:40.772: E/AndroidRuntime(2231): at dalvik.system.NativeStart.main(Native Method)
06-16 14:59:40.772: E/AndroidRuntime(2231): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1755
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.content.res.Resources.getText(Resources.java:244)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.widget.TextView.setText(TextView.java:3888)
06-16 14:59:40.772: E/AndroidRuntime(2231): at chapter.two.hello_world.MainActivity.setStartUpScreenText(MainActivity.java:45)
06-16 14:59:40.772: E/AndroidRuntime(2231): at chapter.two.hello_world.MainActivity.onCreate(MainActivity.java:30)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.Activity.performCreate(Activity.java:5231)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 14:59:40.772: E/AndroidRuntime(2231): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 14:59:40.772: E/AndroidRuntime(2231): ... 11 more
06-16 14:59:50.682: I/Process(2231): Sending signal. PID: 2231 SIG: 9
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="chapter.two.hello_world.MainActivity"
tools:ignore="MergeRootFrame,ExtraText" >
<TextView
android:id="@+id/textView1"
android:layout_marginLeft="5dip"
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="36dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_name_label" />
<TextView
android:id="@+id/textView2"
android:layout_marginLeft="5dip"
android:layout_below="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_mass_label"/>
<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_marginLeft="5dip"
android:layout_below="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_gravity_label"/>
<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_marginLeft="5dip"
android:layout_below="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_colonies_label"/>
<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_marginLeft="5dip"
android:layout_below="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_population_label"/>
<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_marginLeft="5dip"
android:layout_below="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_military_label"/>
<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_marginLeft="5dip"
android:layout_below="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_bases_label"/>
<TextView
android:id="@+id/dataView7"
android:layout_toRightOf="@+id/textView7"
android:layout_alignStart="@+id/dataView1"
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_marginLeft="5dip"
android:layout_below="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet_forcefield_label"/>
<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>
Remove your setContentView()
function call at the end of your function setStartUpScreenText()
. You are doing setContentView
after initializing all values. You must do setText
after
setContentView(R.layout.activity_main);
First call setContentView()
with the xml you want to load. Then, call the initialization functions on the controls present on that view xml.
A parameter to some of the setText()
calls is an int
. Therefore the setText(int)
overload that attempts to set the text with a resource id is used instead of setText(CharSequence)
that uses a string.
To explicitly use setText(CharSequence)
, use e.g. setText(Integer.toString(earth.someintegervalue))
.
Remove setContentView(...)
from setStartUpScreenText()
. It looks like you are setting the text and then setting the screen back to its original state.
You can check also if your Textview
linked to any TextWatcher
or Filter
then trace it.
User contributions licensed under CC BY-SA 3.0