无法将android.widget.LinearLayout转换为android.widget.TextView对话框
我正在尝试制作一个高分列表,可以从我的主菜单访问。我试图通过使用对话框来实现这一点。我的对话类:无法将android.widget.LinearLayout转换为android.widget.TextView对话框,android,xml,android-layout,Android,Xml,Android Layout,我正在尝试制作一个高分列表,可以从我的主菜单访问。我试图通过使用对话框来实现这一点。我的对话类: public class CustonDialog extends Dialog { private LinearLayout layout; private Context mContext; public CustonDialog(Context context) { super(context, android.R.style.Theme_Transl
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>
以及:
以及:
当我运行代码时,当到达void addKeyValuePair的第一行时,它会抛出一个异常android.widget.LinearLayout无法被转换为android.widget.TextView。有人知道如何解决这个问题吗?而不是
写入TextView TextView\u key=TextView findViewByIdR.id.row\u TextView\u值
似乎R.layout.row_键是一个线性布局,它不能被转换为textView,只需将变量与实际的文本视图绑定,即row_textView_值,就像您使用上面的按钮所做的那样
请注意,TextView必须与您在setContentView中添加的布局相同,否则您必须像键入一样将布局包含在XML中
标签
阅读更新后的问题后,这里是更新2
从其他xml文件中删除include您的layouts R.layout.row_键和R.layout.row_值具有作为根容器的LinearLayout。这就是为什么会出现此异常
无法将android.widget.LinearLayout转换为android.widget.TextView对话框
解决方案
1.使您的两个布局仅包含文本视图
<?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_value"
android:id="@+id/row_textView_value"
android:layout_marginTop="5sp"
android:layout_marginLeft="20sp"
android:textSize="30sp">
</TextView>
发布stacktraceI刚刚添加了它。现在我得到了以下信息:java.lang.NullPointerException:尝试在以下位置对空对象引用调用虚拟方法“void android.widget.TextView.setTextjava.lang.CharSequence”com.example.sofie.galgeleg.CustonDialog.addKeyValuePairConstonDialog.java:43在com.example.sofie.galgeleg.MainMenu.onClickMainMenu.java:67是否在同一布局中添加了文本视图?我有单独的xml文件用于行\键和行\值我尝试删除LinearLayout在行\键和行\值中初始化。现在我得到了这样的结果:java.lang.NullPointerException:尝试在com.example.sofie.galgeleg.CustonDialog.addKeyValuePairConstonDialog.java:50上的空对象引用上调用虚拟方法'void android.widget.TextView.setTextjava.lang.CharSequence'在com.example.sofie.galgeleg.MainMenu.onClickMainMenu.java:67中,如果有单独的xml文件,则必须将其他xml文件包含到2中。当我写textView textView_key时,它会给我一个错误,说明在我写row_key.findViewById时它已经在范围中定义;它说它无法解析row_键。行值也是如此。这可能是我的打字错误。只需根据您的布局使用ID即可。我只举了一个例子。您是否尝试过第一个解决方案,它是唯一的xml更改?我遇到了以下异常:java.lang.NullPointerException:尝试在以下位置对空对象引用调用虚拟方法“void android.widget.LinearLayout.addViewandroid.view.view”com.example.sofie.galgeleg.CustonDialog.addKeyValuePairUserStonDialog.java:53位于com.example.sofie.galgeleg.MainMenu.onClickMainMenu.java:67只需在旧代码之前撤消代码并使用第一个解决方案;我看不到你的代码。
<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>
TextView textView_key = (TextView) getLayoutInflater().inflate(R.layout.row_key,layout,false);
TextView textView_value = (TextView) getLayoutInflater().inflate(R.layout.row_value,layout,false);
<include layout="@layout/row_key"/>
<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">
<include layout="@layout/row_value"/>
<include layout="@layout/row_key"/>
</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_value"
android:id="@+id/row_textView_value"
android:layout_marginTop="5sp"
android:layout_marginLeft="20sp"
android:textSize="30sp">
</TextView>
View row_key=getLayoutInflater().inflate(R.layout.row_key,null);
TextView textView_key = (TextView)row_key.findViewById(R.id.textView1) ;
View row_value=getLayoutInflater().inflate(R.layout.row_value,null);
TextView textView_value=(TextView)row_value.findViewById(R.id.textView1);