Pregunta

I have the following compound view which represents a FlashCard and will only be created programmatically.

public class FlashCardView extends LinearLayout {

private FlashCard flashCard;

public FlashCardView(Context context, FlashCard flashCard) {
    super(context);
    this.flashCard = flashCard;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.flashcard, this);

    ((TextView)findViewById(R.id.lbl_lesson)).setText(flashCard.getLesson()); //line 23
    ((TextView)findViewById(R.id.lbl_box)).setText(flashCard.getBox());

}
}

The xml I'm inflating looks like this:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_vertical|center_horizontal"
        android:layout_weight="1">

    <TextSwitcher
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textSwitcher"
            >

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/germanText"
                android:textSize="24sp"
                android:textIsSelectable="false"
                />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/spanishText"
                android:textSize="24sp"/>
    </TextSwitcher>
</LinearLayout>

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/lbl_box"
            android:layout_weight="1"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/lbl_lesson"
            android:layout_weight="1"
            android:gravity="right"/>
</LinearLayout>
</merge>

But when trying to find a child view with findViewById() I get the following exception on the first findViewById call:

04-12 00:17:20.829: ERROR/AndroidRuntime(31308): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{ch.crisi.congusto_a2/ch.crisi.congusto_a2.ConGustoA2}: android.content.res.Resources$NotFoundException: String resource ID #0x1
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
    at android.content.res.Resources.getText(Resources.java:230)
    at android.widget.TextView.setText(TextView.java:3769)
    at ch.crisi.congusto_a2.FlashCardView.<init>(FlashCardView.java:23)
    at ch.crisi.congusto_a2.ConGustoA2.onCreate(ConGustoA2.java:30)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    ... 11 more

I also tried this approach:

    View v = inflater.inflate(R.layout.flashcard, null, false);
    v.findViewById(...)

..and read a lot on SO, but have not found the cause of the problem so far. I'm working with IntelliJ IDEA and also did a rebuild of the project.

update I changed to root element of the flashcard xml to linearlayout and inflate and set the text now as follows and it works. Thanks for all the comments!

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.flashcard, null, false);

    TextView tv = (TextView) v.findViewById(R.id.lbl_lesson);
    tv.setText(String.valueOf(flashCard.getLesson()));

    tv = (TextView) v.findViewById(R.id.lbl_box);
    tv.setText(String.valueOf(flashCard.getBox()));
¿Fue útil?

Solución 2

Make sure your method getLesson() is returning a string. If it returns int you would get this exact error because the setText() method assumes you are trying to set a string resource from R.

Try with ((TextView)findViewById(R.id.lbl_lesson)).setText(Integer.toString(flashCard.getLesson()));

Otros consejos

Solution (see comments):

Find the TextView with:

View v = inflater.inflate(R.layout.flashcard, null, false);
TextView tv = (TextView)v.findViewById(R.id.lbl_lesson);

And make sure you're setting the text on the TextView with a String, not a direct resource (int).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top