【问题标题】:TextView.setText() isn't workingTextView.setText() 不起作用
【发布时间】:2014-06-16 18:13:36
【问题描述】:

我的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> 

【问题讨论】:

标签: android textview settext


【解决方案1】:

一些setText() 调用的参数是int。因此,setText(int) 重载尝试使用资源 id 设置文本,而不是使用字符串的 setText(CharSequence)

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

【讨论】:

  • 我做了:String.valueOf(),但仍然无法正常工作,应用程序打开而不是立即关闭。
  • 查看 logcat 以了解违规 setText() 的特定代码行或代码中的任何其他问题。
  • 谢谢老兄!我忘记将 String.valueOf() 设为其中一个值。
【解决方案2】:

在函数setStartUpScreenText() 的末尾删除你的setContentView() 函数调用。初始化所有值后,您正在执行setContentView。你必须在之后做setText

setContentView(R.layout.activity_main);

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

【讨论】:

  • 你在看什么代码?在 OP 的问题中,setContentView(...) 显然在 super.onCreate(...) 之后立即被调用
  • 他最后也在函数setStartUpScreenText()中再次调用。
  • 我从 setStartUpScreenText() 中删除了 setContentView() 并修改了 onCreate() 方法 botm : 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();现在应用程序在打开后关闭......任何人都可以帮忙吗?
  • 我也放了 LogCat 内容和 activity_main.xml 内容!感谢您的帮助
  • 它抱怨缺少一些资源。你能试着清理你的项目一次吗?在 Eclipse 中,转到 Project > Clean... 2.选择您的项目,然后按 OK 3.重新启动应用程序。如果再次发生,请删除 r.java 文件。它会自动生成。
【解决方案3】:

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

【讨论】:

  • 我从 setStartUpScreenText() 中删除了 setContentView() 并修改了 onCreate() 方法,在底部添加了:setStartUpScreenText();现在应用程序在打开后关闭...有人可以帮忙吗?
  • 将崩溃的logcat输出和activity_main.xml的内容添加到你的问题中
【解决方案4】:

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

android TextView setText not working

【讨论】:

    猜你喜欢
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多