【问题标题】:android.widget.LinearLayout cannot be cast to android.widget.TextView dialogandroid.widget.LinearLayout 无法转换为 android.widget.TextView 对话框
【发布时间】: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


【解决方案1】:

您的布局 R.layout.row_keyR.layout.row_valueLinearLayout 作为根容器。这就是您收到此异常的原因。

android.widget.LinearLayout 无法转换为 android.widget.TextView 对话框

解决方案
1.让你的两个布局都只包含TextView

 <?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>
  1. 膨胀布局时,您可以进入视图。

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

【讨论】:

  • 至 2. 当我写“textView textView_key”时它给了我一个错误,说它已经在范围内定义当我写“row_key.findViewById();”时它说它无法解析row_key。 row_value 也是如此
  • 这可能是我的一些拼写错误。只需根据您的布局使用 id。我只是给你一个例子。您是否尝试过仅更改 xml 的第一个解决方案
  • 现在我得到了这个异常:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on an null object reference at com. example.sofie.galgeleg.CustonDialog.addKeyValuePair(CustonDialog.java:53) at com.example.sofie.galgeleg.MainMenu.onClick(MainMenu.java:67)
  • 只需撤消您的代码直到旧代码并使用第一个解决方案。
  • 你在哪里初始化 private LinearLayout layout; 我在你的代码中没有看到。
【解决方案2】:

代替

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 textView_key = (TextView) findViewById(R.id.row_textView_value)

似乎R.layout.row_key 是一个线性布局,它不会被强制转换为 textView 只是将变量与row_textView_value 的实际文本视图绑定,就像你对上面的按钮所做的那样 并注意 textviews 必须与您在 setContentView 中添加的布局相同,否则您必须在 XML 中包含布局,例如键入 &lt;include /&gt;标签

<include layout="@layout/row_key"/>

阅读更新的问题后,这里是更新 2

<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 文件中删除包含

【讨论】:

  • 现在我明白了:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on an null object reference at com.example .sofie.galgeleg.CustonDialog.addKeyValuePair(CustonDialog.java:43) at com.example.sofie.galgeleg.MainMenu.onClick(MainMenu.java:67)
  • 你是否在相同的布局中添加了textViews?
  • 我有单独的 xml 文件用于 row_key 和 row_value
  • 我尝试删除 row_key 和 row_value 中的 LinearLayout 初始化。现在我明白了: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on an null object reference at com.example.sofie.galgeleg.CustonDialog.addKeyValuePair (CustonDialog.java:50) 在 com.example.sofie.galgeleg.MainMenu.onClick(MainMenu.java:67)
  • 如果你在单独的 xml 中拥有它,那么你必须包含其他 XML 文件
猜你喜欢
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多