TextView.setText()不起作用

时间:2014-06-16 18:13:37

标签: android textview settext

我的TextView.setText()无效。

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

/ 所以我的setText()方法不会更改XML文件中的默认值。 我应该将setStartUpScreenText()方法的内容放在onCreate方法中吗?它可以在外面工作吗? /

现在应用程序在运行后关闭。

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> 

4 个答案:

答案 0 :(得分:1)

在功能结束时移除setContentView()函数调用setStartUpScreenText()。初始化所有值后,您正在执行setContentView。您必须在

之后setText
setContentView(R.layout.activity_main);

首先使用要加载的xml调用setContentView()。然后,在该视图xml上的控件上调用初始化函数。

答案 1 :(得分:0)

setContentView(...)移除setStartUpScreenText()。看起来您正在设置文本,然后将屏幕设置回原始状态。

答案 2 :(得分:0)

某些setText()来电的参数是int。因此,将使用尝试使用资源ID设置文本的setText(int)重载,而不是使用字符串的setText(CharSequence)

要明确使用setText(CharSequence),请使用例如setText(Integer.toString(earth.someintegervalue))

答案 3 :(得分:0)

您还可以检查您的Textview是否链接到任何TextWatcherFilter,然后进行跟踪。

android TextView setText not working