java - ConstraintLayout 无法转换为 android.widget.TextView

标签 java android casting adapter

当我尝试启动 Activity 时,不断收到运行时错误。

发生错误的行:

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

日志猫:

java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView at 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;
    }
}

最佳答案

来自documentation :

View: The view within the AdapterView that was clicked (this will be a view provided by the adapter)

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

字符串信息 = v.findViewById(R.id.textView).getText().toString()

关于java - ConstraintLayout 无法转换为 android.widget.TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47702186/

相关文章:

java - 如何使 ListView 在 Android 中不发出点击声音?

Mac OSX 上的 Java 7(更新 7 和 9)- 小程序打印小字体

android - 在 android 中播放来自 youtube 的视频文件

java - 类似于java中的fork()函数

c - AVR GCC - 类型转换问题

java - TableCellEditor 类不起作用

java - 使用 apache POI 在 MS Word 文档中添加文本

android - 如何从自定义 ListView 中的 PopupMenu 获取项目的位置

c# - 将抽象转换为由 Derived 实现的接口(interface)

java - 如何在 Clojure 中转换 Java 类?