我正在使用 Gridview 在内部显示一些 TextView。我创建了一个清除按钮,它设置了一个带有空 ArrayList 的新适配器,它工作正常。当我试图在另一个地方执行相同的操作时,会发生异常。我已经跟踪了我的所有代码,直到我指出了导致问题的语句。
在第一堂课上声明 gridview
GridView gridView;
在 OnCreate 方法中有参考
gridView = (GridView) findViewById(R.id.gridView1);
创建适配器
String[] arrayEmpty = new String[] {"", "", "", "", "", "", "", "", ""};
ArrayList<String> list = new ArrayList<String>(Arrays.asList(arrayEmpty));
gridView.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,list));//list_item is acustomized layout for the TextView
我在 Clear() 方法中所做的并且工作正常:
gridView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.list_item,listEmpty));
我正在做什么来设置值并且不起作用:
ArrayList<String> gridList= new ArrayList<String>();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
gridList.add(String.valueOf(e[i][j].getValue()));
}
}
//The next statement cause an Exception..Why!!?
gridView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.list_item,gridList));
class solveTask extends AsyncTask<Object, Void, String> {
@Override
protected void onPreExecute() {
ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setProgress(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Solving, please wait a few seconds...");
pd.setIndeterminate(true);
pd.setCancelable(false);
pd.show();
}
@Override
protected String doInBackground(Object... parametros) {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
fillSudoku();
solve();
}
});
return null;
}
@Override
protected void onPostExecute(String result)
{ //update progressDialog here
pd.dismiss();
}
}
一些日志:
04-27 10:24:04.556: W/dalvikvm(7823): threadid=9: 线程以未捕获的异常退出 (group=0x40015560) 04-27 10:24:04.586: E/AndroidRuntime(7823): 致命异常: AsyncTask #1 04-27 10:24:04.586:E / AndroidRuntime(7823):java.lang.RuntimeException:执行doInBackground()时发生错误04-27 10:24:04.586:E / AndroidRuntime(7823):在android .os.AsyncTask$3.done(AsyncTask.java:200) 04-27 10:24:04.586: E/AndroidRuntime(7823): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 04 -27 10:24:04.586: E/AndroidRuntime(7823): 在 java.util.concurrent.FutureTask.setException(FutureTask.java:125) 04-27 10:24:04.586: E/AndroidRuntime(7823): 在 java .util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 04-27 10:24:04.586: E/AndroidRuntime(7823): at java.util.concurrent。FutureTask.run(FutureTask.java:138) 04-27 10:24:04.586: E/AndroidRuntime(7823): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 04-27 10:24: 04.586: E/AndroidRuntime(7823): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 04-27 10:24:04.586: E/AndroidRuntime(7823): 在 java.lang.Thread .run(Thread.java:1019) 04-27 10:24:04.586: E/AndroidRuntime(7823): 由: java.lang.UnsupportedOperationException: 在 AdapterView 04-27 10:24:04.586 中不支持 removeAllViews() : E/AndroidRuntime(7823): at android.widget.AdapterView.removeAllViews(AdapterView.java:511) 04-27 10:24:04.586: E/AndroidRuntime(7823): at com.etaworx.thesudokusolver.MainActivity.printSudoku( MainActivity.java:481) 04-27 10:24:04.586: E/AndroidRuntime(7823): at com.etaworx。thesudokusolver.MainActivity.solve(MainActivity.java:579) 04-27 10:24:04.586: E/AndroidRuntime(7823): at com.etaworx.thesudokusolver.MainActivity$solveTask.doInBackground(MainActivity.java:625) 04-27 10:24:04.586: E/AndroidRuntime(7823): 在 com.etaworx.thesudokusolver.MainActivity$solveTask.doInBackground(MainActivity.java:1) 04-27 10:24:04.586: E/AndroidRuntime(7823): 在 android .os.AsyncTask$2.call(AsyncTask.java:185) 04-27 10:24:04.586: E/AndroidRuntime(7823): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 04 -27 10:24:04.586: E/AndroidRuntime(7823): ... 4 更多 04-27 10:24:09.849: E/WindowManager(7823): Activity com.etaworx.thesudokusolver.MainActivity 已泄露窗口 com.android .internal.policy.impl.PhoneWindow$DecorView@4054e258 最初添加在这里 04-27 10:24:09.849:E/WindowManager(7823): android.view.WindowLeaked: Activity com.etaworx.thesudokusolver.MainActivity 已经泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@4054e258 04-27 10:24 :09.849: E/WindowManager(7823): 在 android.view.ViewRoot.(ViewRoot.java:258) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl .java:148) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 04-27 10:24:09.849: E/WindowManager(7823) ): 在 android.view.Window$LocalWindowManager.addView(Window.java:424) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.app.Dialog.show(Dialog.java:241) 04-27 10:24:09.849: E/WindowManager(7823): at com.etaworx.thesudokusolver.MainActivity。showLoading(MainActivity.java:570) 04-27 10:24:09.849: E/WindowManager(7823): at com.etaworx.thesudokusolver.MainActivity$2.onClick(MainActivity.java:77) 04-27 10:24:09.849 : E/WindowManager(7823): 在 android.view.View.performClick(View.java:2485) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.view.View$PerformClick.run( View.java:9080) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.os.Handler.handleCallback(Handler.java:587) 04-27 10:24:09.849: E/WindowManager( 7823): 在 android.os.Handler.dispatchMessage(Handler.java:92) 04-27 10:24:09.849: E/WindowManager(7823): 在 android.os.Looper.loop(Looper.java:123) 04 -27 10:24:09.849: E/WindowManager(7823): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-27 10:24:09.849: E/WindowManager(7823): at java.lang .reflect.方法。invokeNative(本机方法)04-27 10:24:09.849:E/WindowManager(7823):在 java.lang.reflect.Method.invoke(Method.java:507)04-27 10:24:09.849:E/WindowManager (7823): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-27 10:24:09.849: E/WindowManager(7823): 在 com.android.internal.os。 ZygoteInit.main(ZygoteInit.java:597) 04-27 10:24:09.849: E/WindowManager(7823): at dalvik.system.NativeStart.main(Native Method)E/WindowManager(7823): 在 dalvik.system.NativeStart.main(Native Method)E/WindowManager(7823): 在 dalvik.system.NativeStart.main(Native Method)