无法在自定义 ListView 适配器上设置自定义 Toast
Unable to set a Custom Toast on Custom ListView Adapter
我为自定义 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;
}
}
XML 自定义 TOAST -
<?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
)
我为自定义 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;
}
}
XML 自定义 TOAST -
<?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
)