所以我有一个12 x 9网格的“按钮”称为“瓷砖”.
Tile.java扩展了小部件“button”.
我现在遇到了一个问题,我正试图让按钮显示一个回忆按下按钮ID的吐司.
网格是动态添加的,我希望它保持这种状态.
这是GameBoardActivity.java的代码:
这使得“瓷砖”的12×9网格为每个网格添加了一个监听器.
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: main02-22 20:45:14.623: E/AndroIDRuntime(7868): java.lang.RuntimeException: setVIEw must have been called02-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.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而且它不知道视图已经是什么?
另外,Eclipse不会因为某些原因让我添加setVIEw().我确定我在这里遇到了封装问题,但我对它们是什么感到困惑.
感谢您的帮助.
JRad
解决方法:
将下面的代码行更改为以下代码:
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的引用.相反,您的原始代码会创建两个Toasts,并尝试“显示”尚未设置其视图的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;}
总结 以上是内存溢出为你收集整理的java – “必须调用setView” – 自定义类扩展按钮全部内容,希望文章能够帮你解决java – “必须调用setView” – 自定义类扩展按钮所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)