ConstraintLayout无法转换为android.widget.TextView [英] ConstraintLayout cannot be cast to android.widget.TextView

查看:43
本文介绍了ConstraintLayout无法转换为android.widget.TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试启动活动时,我总是收到运行时错误.

I keep getting a runtime error when I try to launch an 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);
    }
};

Logcat:

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

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)

这是怎么回事?直到我开始在自定义行布局中使用自定义数组适配器之前,一切都很好.

What is happening here? It was fine until I started to use a custom array adapter with custom row layout.

custom_row.xml

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

推荐答案

来自

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

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

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

In your case the View is the root ConstraintLayout. And as the error says you can't cast it to TextView. To get the info you can use:

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

这篇关于ConstraintLayout无法转换为android.widget.TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆