我有以下复合视图,它代表一个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,并且还重建了该项目。
更新 我更改为flashcard xml的根元素到linearlayout并膨胀并现在设置文本如下,它的工作原理。感谢所有评论!
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()));
答案 0 :(得分:3)
解决方案(见评论):
使用以下内容查找TextView
View v = inflater.inflate(R.layout.flashcard, null, false);
TextView tv = (TextView)v.findViewById(R.id.lbl_lesson);
并确保您使用TextView
设置String
上的文字,而不是直接资源(int
)。
答案 1 :(得分:2)
确保您的方法getLesson()
返回string
。如果它返回int
,您将收到此确切错误,因为setText()
方法假定您尝试从R设置字符串资源。
尝试使用((TextView)findViewById(R.id.lbl_lesson)).setText(Integer.toString(flashCard.getLesson()));