ConstraintLayout cannot be cast to android.widget.TextView

风流意气都作罢 提交于 2021-02-05 09:35:37

问题


I keep getting a runtime error when I try to launch an activity.

Line where the error happen:

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 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.

Edit:

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>

My adapter:

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

回答1:


From the documentation:

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

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:

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




回答2:


You just got wrong id for your constrain layout and mixed it up with textview that you have make declaration in MainActivity



来源:https://stackoverflow.com/questions/47702186/constraintlayout-cannot-be-cast-to-android-widget-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!