问题描述
我为自定义 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
的引用)