微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在自定义 ListView 适配器上设置自定义 Toast

如何解决无法在自定义 ListView 适配器上设置自定义 Toast

我为自定义 toast 制作了 xml,当我在自定义 toast 中对文本和图像进行硬编码时一切正常,但是在根据列表项更改它们时,我收到“尝试调用虚拟方法 'void android.空对象引用上的 widget.TextView.setText(java.lang.CharSequence)'

自定义适配器 -

package com.example.android.l4q13;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class VersionAdapter extends ArrayAdapter<Version> {
    private Context mContext;
    private int mResource;
    TextView tvt;
    ImageView ivt;
    LayoutInflater mInflater;

    public VersionAdapter(@NonNull Context context,int resource,@NonNull ArrayList<Version> objects) {
        super(context,resource,objects);
        mContext=context;
        mResource=resource;
        this.tvt=tvt;
        this.ivt=ivt;
    }
//    Context context=mContext.getApplicationContext();
//    mInflater = LayoutInflater.;
    @NonNull
    @Override
    public View getView(int position,@Nullable View convertView,@NonNull ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        String version = getItem(position).getVersion();
        int img = getItem(position).getImg();

        Version v = new Version(version,img);
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        convertView = layoutInflater.inflate(mResource,parent,false);

        Button tvversion = (Button) convertView.findViewById(R.id.button);
        ImageView tvimg = (ImageView) convertView.findViewById(R.id.imageView1);
        tvversion.setText(version);
        tvimg.setimageResource(img);

        tvversion.setonClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Code here executes on main thread after user presses button

                //Toast.makeText(getContext(),ver1
                // s1ion,Toast.LENGTH_SHORT).show();

                Toast toast = new Toast(mContext);
                View view = inflater.inflate(R.layout.cust_toast,null);

                TextView tvt = (TextView) v.findViewById(R.id.textView1t);
                ImageView ivt = (ImageView) v.findViewById(R.id.imageView1t);



                tvt.setText(version);
                ivt.setimageResource(img);
                toast.setView(view);

                toast.setDuration(Toast.LENGTH_SHORT);
                toast.show();
            }
        });

        return convertView;

    }
}

用于自定义 Toast 的 XML -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout1"
    android:background="@android:color/white">

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1t"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:text="Cupcake"
        android:textColor="@android:color/black">
    </TextView>

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_below="@+id/textView1t"
        android:layout_margin="5dip"
        android:src="@drawable/cup"
        android:id="@+id/imageView1t">
    </ImageView>

    <TextView
        android:id="@+id/textView2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="This is the demo of Custom Toast Notification"
        android:gravity="center"
        android:layout_below="@+id/imageView1t"
        android:textColor="@android:color/black">
    </TextView>

</RelativeLayout>

解决方法

使用 v 代替 view

 TextView tvt = (TextView) view.findViewById(R.id.textView1t);
 ImageView ivt = (ImageView) view.findViewById(R.id.imageView1t);

因为这些 id 是视图的子视图,而不是 v(这是对按钮 tvversion 的引用)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?