将以下代码行更改为以下代码行:
Toast toast = new Toast(context);
Toast.makeText(context, tileID, Toast.LENGTH_SHORT);
toast.show();
更改为:
Toast toast = Toast.makeText(context, tileID, Toast.LENGTH_SHORT);
toast.show();
从源代码中可以看到,只有当mNextView为null时才会引发该异常。
假设函数“makeText”设置了它,它确实设置了,但您的原始代码没有捕获对它构建的Toast的引用。相反,原始代码创建两个Toast,并尝试“显示”尚未设置视图的Toast。
public void show() {
if (mNextView == null) {
throw new RuntimeException("setView must have been called");
}
....
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);
result.mNextView = v;
result.mDuration = duration;
return result;
}