2010-08-30 15 views
18

मैं एंड्रॉइड में अपना पहला कदम उठा रहा हूं और एक बहुत ही सरल ऐप से शुरू कर रहा हूं जो बुनाई पैटर्न के माध्यम से प्रगति का ट्रैक रखता है और प्रासंगिक पंक्ति के लिए निर्देश दिखाता है।संसाधन नहीं मिला TextView

मैं प्रोग्रामेटिक रूप से कुछ टेक्स्टव्यू ऑब्जेक्ट्स को अपडेट करना चाहता हूं। हालांकि, getViewById() का उपयोग करके उन्हें ठीक से पहचानने की प्रतीत नहीं होती है और ऐप क्रैश हो जाता है।

Google पर खोज करने के बाद ऐसा लगता है कि लेआउट एक्सएमएल में एक्सएमएल नेमस्पेस के साथ कभी-कभी समस्याएं होती हैं लेकिन मेरा ठीक दिखता है। क्या यह गुंजाइश के साथ कुछ करने के लिए है, शायद?

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/लेआउट/main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
     android:id="@+id/row" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Row" 
     /> 
    <TextView 
     android:id="@+id/instr" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Instructions" 
     android:layout_below="@id/row" 
     /> 
    <Button 
     android:id="@+id/increment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="+" 
     android:layout_alignParentBottom="true" /> 
    <Button 
     android:id="@+id/decrement" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="-" 
     android:layout_toRightOf="@id/increment" 
     android:layout_alignParentBottom="true" /> 
    <TextView 
     android:id="@+id/stitchremain" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="stitches remaining" 
     android:layout_above="@id/increment" 
     /> 
</RelativeLayout> 

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; 
} 

यहां डीडीएमएस में दिखाया गया त्रुटि संदेश है।

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] 

आपकी मदद के लिए बहुत धन्यवाद।

उत्तर

78

mRow एक पूर्णांक है। जब आप लाइन 75 पर setText (mRow) को कॉल करते हैं, तो ऐसा लगता है कि आप टेक्स्ट को स्ट्रिंग संसाधन के साथ आईडी = MRow के मान के साथ सेट करने का प्रयास कर रहे हैं।

इसके बजाय, कार्य करें:

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

है कि यह तय की तरह - बहुत बहुत धन्यवाद! –

+0

हे उसी प्रोब में फंस गया था! धन्यवाद ! +1 – GoodSp33d

+0

इसके लिए धन्यवाद: डी +1 – jtwigg

4

तुम हमेशा यह TextView को सेट करने से पहले स्ट्रिंग के लिए अन्य मूल्यों को परिवर्तित करना चाहिए,

txtX.setText(Integer.toString(intVal)); 
संबंधित मुद्दे