【发布时间】:2018-01-07 12:13:57
【问题描述】:
我正在尝试制作一个高分列表,可以从我的主菜单访问。我正在尝试通过使用对话框来实现这一点。我的对话类:
public class CustonDialog extends Dialog {
private LinearLayout layout;
private Context mContext;
public CustonDialog(Context context) {
super(context, android.R.style.Theme_Translucent_NoTitleBar);
setContentView(R.layout.activity_highscore);
setCancelable(false);
mContext = context;
Button buttonClose = (Button) findViewById(R.id.button_close);
buttonClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
public void addKeyValuePair(String key, String value) {
/* View row_key = getLayoutInflater().inflate(R.layout.row_key,layout,false);
TextView textView_key = (TextView) row_key.findViewById(R.id.row_textView_key);
View row_value = getLayoutInflater().inflate(R.layout.row_value,layout,false);
TextView textView_value = (TextView) row_value.findViewById(R.id.row_textView_value);
*/
// TextView textView_key = (TextView) findViewById(R.id.row_textView_value);
TextView textView_key = (TextView) getLayoutInflater().inflate(R.layout.row_key,layout,false);
TextView textView_value = (TextView) getLayoutInflater().inflate(R.layout.row_value,layout,false);
textView_key.setText(key);
textView_value.setText(value);
layout.addView(textView_key);
layout.addView(textView_value);
}
}
带有xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button_close"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textStyle="bold" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
和:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dummy_text"
android:id="@+id/row_textView_key"
android:layout_marginTop="20sp"
android:textStyle="bold"
android:textSize="30sp"
android:layout_marginLeft="20sp">
<include layout="@layout/row_value"/>
</TextView>
和:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dummy_value"
android:id="@+id/row_textView_value"
android:layout_marginTop="5sp"
android:layout_marginLeft="20sp"
android:textSize="30sp">
<include layout="@layout/row_key"/>
</TextView>
当我运行代码时,当到达 void addKeyValuePair 的第一行时,它会抛出异常“android.widget.LinearLayout cannot be cast to android.widget.TextView。有人知道如何解决这个问题吗?
【问题讨论】:
-
发布堆栈跟踪
-
我刚刚添加了。
标签: android xml android-layout