gpt4 book ai didi

java - "setView must have been called"- 自定义类扩展按钮

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:00 25 4
gpt4 key购买 nike

所以我有一个 12 x 9 的“按钮”网格,称为“图 block ”。Tile.java 扩展了小部件“按钮”。

不过,我现在遇到了一个问题,我正在尝试让按钮显示一个 toast,它可以记忆起按下的按钮的 ID。

网格是动态添加的,我希望它保持这种状态。

这是来自 GameBoardActivity.java 的代码:

这会生成 12x9 的“方 block ”网格并为每个方 block 添加一个监听器。

    public void gridRowButtons(int iterations){
final Tile[] gridSpaces = new Tile[12];
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
for (int i = 0; i < 12; i++) {
String idString = String.valueOf(aToI[iterations]) + String.valueOf(i + 1);
final int id = getResources().getIdentifier(idString, "id", getPackageName());
gridSpaces[i] = new Tile(getApplicationContext());
gridSpaces[i].setText("");
gridSpaces[i].setHeight(40);
gridSpaces[i].setWidth(40);
gridSpaces[i].setLayoutParams(buttonParams);
gridSpaces[i].setId(id);
OnClickListener showID = new OnClickListener(){
public void onClick(View view){
TextView text = (TextView) findViewById(R.id.tileIDText);
String tileID = getApplicationContext().getResources().getResourceEntryName(id);
text.setText(tileID);
Tile clickedTile = (Tile) findViewById(id);
clickedTile.tileClick(0, tileID);
}
};
gridSpaces[i].setOnClickListener(showID);
}
LinearLayout buttonRow = new LinearLayout(getApplicationContext());
buttonRow.setOrientation(LinearLayout.HORIZONTAL);
buttonRow.setLayoutParams(buttonParams);
LinearLayout boardSpace = (LinearLayout) this.findViewById(R.id.boardLayout);
for (int i = 0; i < gridSpaces.length; i++) {
buttonRow.addView(gridSpaces[i]);
}
boardSpace.addView(buttonRow);
}

这里是上面提到的 tileClick 方法:

    public void tileClick(int action, String tileID) {
switch(action) {
case 1 :
//action 1
case 2 :
//action 2
default :
Context context = getContext();
Toast toast = new Toast(context);
Toast.makeText(context, tileID, Toast.LENGTH_SHORT);
toast.show();
}
}

LogCat 显示以下内容:

02-22 20:45:14.623: E/AndroidRuntime(7868): FATAL EXCEPTION: main
02-22 20:45:14.623: E/AndroidRuntime(7868): java.lang.RuntimeException: setView must have been called
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.widget.Toast.show(Toast.java:103)
02-22 20:45:14.623: E/AndroidRuntime(7868): at com.jneal.ecquire.Tile.tileClick(Tile.java:51)
02-22 20:45:14.623: E/AndroidRuntime(7868): at com.jneal.ecquire.GameBoardActivity$1.onClick(GameBoardActivity.java:55)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.view.View.performClick(View.java:3627)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.view.View$PerformClick.run(View.java:14329)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.os.Handler.handleCallback(Handler.java:605)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.os.Looper.loop(Looper.java:137)
02-22 20:45:14.623: E/AndroidRuntime(7868): at android.app.ActivityThread.main(ActivityThread.java:4511)
02-22 20:45:14.623: E/AndroidRuntime(7868): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:45:14.623: E/AndroidRuntime(7868): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 20:45:14.623: E/AndroidRuntime(7868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
02-22 20:45:14.623: E/AndroidRuntime(7868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
02-22 20:45:14.623: E/AndroidRuntime(7868): at dalvik.system.NativeStart.main(Native Method)

这是怎么回事?我在我的 MainActivity.java 中获得了几乎完全相同的用于显示 toast 的代码,并且可以毫无问题地执行。是因为我扩展了 Button 并且它不知道 View 已经是什么了吗?此外,Eclipse 出于某种原因不允许我添加 setView() 。我确定我这里有封装问题,但我对它们是什么感到困惑。提前感谢您的帮助。杰拉德

最佳答案

将下面的代码行更改为以下代码行:

        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,并尝试“显示”尚未设置其 View 的那个。

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

关于java - "setView must have been called"- 自定义类扩展按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21963146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com