0

我有以下表示 FlashCard 的复合视图,并且只能以编程方式创建。

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());

}
}

我正在膨胀的 xml 看起来像这样:

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

但是,当尝试使用 findViewById() 查找子视图时,我在第一次 findViewById 调用时收到以下异常:

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

我也尝试过这种方法:

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

..并阅读了很多关于SO的内容,但到目前为止还没有找到问题的原因。我正在与 IntelliJ IDEA 合作,并且还对项目进行了重建。

更新 我将抽认卡 xml 的根元素更改为线性布局和膨胀,现在将文本设置如下,它可以工作。感谢所有的评论!

    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()));
4

2 回答 2

3

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).

于 2013-04-12T08:47:23.280 回答
2

确保您的方法getLesson()返回一个string. 如果它返回int,您将得到这个确切的错误,因为该setText()方法假定您正在尝试从 R 设置字符串资源。

尝试((TextView)findViewById(R.id.lbl_lesson)).setText(Integer.toString(flashCard.getLesson()));

于 2013-04-12T08:42:51.477 回答