找不到资源TextView

我正在迈出Android的第一步,我开始使用一个非常简单的应用程序,它通过编织模式跟踪进度,并显示相关行的说明。

我想以编程方式更新几个TextView对象。 但是,使用getViewById()似乎无法正确识别它们并且应用程序崩溃。

在Google上进行搜索后,布局XML中的XML命名空间有时会出现问题,但我看起来还不错。 这可能与范围有关吗?

instructions.java (这是唯一的活动)

package uk.co.oketchup.blanketsquare; import android.app.Activity; import android.os.Bundle; import android.content.SharedPreferences; import android.widget.Button; import android.widget.TextView; import android.view.View.OnClickListener; import android.view.View; public class instructions extends Activity { private int mRow; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* retrieve row from shared preferences, or start from zero if nothing there */ SharedPreferences settings = getPreferences(MODE_PRIVATE); mRow = settings.getInt("row",0); setContentView(R.layout.main); /* associate onClick listeners with the two buttons */ final Button btnIncrement = (Button) findViewById(R.id.increment); btnIncrement.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Increment row ++mRow; calcAndUpdate(); } }); final Button btnDecrement = (Button) findViewById(R.id.decrement); btnDecrement.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Decrement row --mRow; calcAndUpdate(); } }); } private void calcAndUpdate() { String Instructions; int knit1; int knit2; int remaining; if (mRow%2 == 1 ) { /* Odd row */ knit1 = 40 - mRow; Instructions = "K" + knit1; remaining = knit1; } else { /* Even row */ knit1 = 18 - mRow/2; knit2 = knit1 + 1; Instructions = "Sl 1, k" + knit1 + ", [sl 1 kwise] 2 times, k1, p2sso, k" + knit2; remaining = knit1 + knit2 + 2; } /* Update the display */ TextView tRow = (TextView) findViewById(R.id.row); TextView tInstr = (TextView) findViewById(R.id.instr); TextView tStRem = (TextView) findViewById(R.id.stitchremain); /* Set the text */ tRow.setText(mRow); tInstr.setText(Instructions); tStRem.setText(remaining); } } 

/ res / layout / main.xml

     

TextView对象似乎已经注册,因为它们出现在R.java中

 public static final class id { public static final int decrement=0x7f050003; public static final int increment=0x7f050002; public static final int instr=0x7f050001; public static final int row=0x7f050000; public static final int stitchremain=0x7f050004; } 

这是ddms中显示的错误消息。

 Uncaught handler: thread main exiting due to uncaught exception android.content.res.Resources$NotFoundException: String resource ID #0x1 at android.content.res.Resources.getText(Resources.java:200) at android.widget.TextView.setText(TextView.java:2813) at uk.co.oketchup.blanketsquare.instructions.calcAndUpdate(instructions.java:75) at uk.co.oketchup.blanketsquare.instructions.access$100(instructions.java:11) at uk.co.oketchup.blanketsquare.instructions$1.onClick(instructions.java:33) [etc] 

非常感谢您的帮助。

Solutions Collecting From Web of "找不到资源TextView"

mRow是一个整数。 当您在第75行调用setText(mRow)时,它认为您正在尝试使用ID = mRow值的String资源设置文本。

相反,做:

 tRow.setText(Integer.toString(mRow)); 

在将其设置为textview之前,应始终将其他值转换为字符串,例如

  txtX.setText(Integer.toString(intVal));