【问题标题】:ConstraintLayout cannot be cast to android.widget.TextViewConstraintLayout 无法强制转换为 android.widget.TextView
【发布时间】:2017-12-07 19:14:02
【问题描述】:

我在尝试启动活动时不断收到运行时错误。

发生错误的行:

private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {

        textView1.setText("Csatlakozás...");
        String info = ((TextView) v).getText().toString(); // <-- ERROR HERE
        String address = info.substring(info.length() - 17);
        Intent i = new Intent(DeviceListActivity.this, MainActivity.class);
        i.putExtra(EXTRA_DEVICE_ADDRESS, address);
        startActivity(i);
    }
};

Logcat:

java.lang.ClassCastException: android.support.constraint.ConstraintLayout 不能被强制转换为 android.widget.TextView 在 arduinosensors.example.com.smarthome3.DeviceListActivity$1.onItemClick(DeviceListActivity.java:106)

这里发生了什么?在我开始使用具有自定义行布局的自定义数组适配器之前,一切都很好。

编辑:

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textSize="18sp" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

我的适配器:

public class UsersAdapter extends ArrayAdapter<Adapter> {
    public UsersAdapter(Context context, ArrayList<Adapter> users) {
        super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Adapter user = getItem(position);

        if (convertView == null) {
            LayoutInflater sontInflater = LayoutInflater.from(getContext());
            convertView = sontInflater.inflate(R.layout.custom_row,parent,false);
        }

        TextView one = (TextView) convertView.findViewById(R.id.textView);
        TextView two = (TextView) convertView.findViewById(R.id.textView2);

        one.setText(user.name);
        two.setText(user.hometown);

        return convertView;
    }
}

【问题讨论】:

  • 你清理和重建了吗?
  • @tyczj 是的,没用
  • 发布更多代码以便更好地理解
  • 我已经添加了相关代码,但也有完整项目的链接。

标签: java android casting adapter


【解决方案1】:

来自documentation:

View:AdapterView 中被点击的视图(这将是一个 适配器提供的视图)

在您的情况下,View 是根 ConstraintLayout。正如错误所说,您不能将其转换为TextView。要获取您可以使用的信息:

String info = v.findViewById(R.id.textView).getText().toString()

【讨论】:

  • 效果很好,谢谢!无论如何 getText() 在这里不起作用
  • 也许您需要将其转换为TextView
  • 是的,刚刚想通了,谢谢,你们真的很有帮助!
【解决方案2】:

您的约束布局的 id 错误,并将其与您在 MainActivity 中声明的 textview 混在一起

【讨论】:

    猜你喜欢
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2012-06-26
    • 2014-08-28
    相关资源
    最近更新 更多