在Gridview中更改适配器值会导致异常

时间:2013-05-03 12:00:15

标签: android android-gridview

我正在使用Gridview在内部显示一些TextView。我创建了一个清除按钮,用新的ArrayList设置一个新的适配器,它工作正常。当我尝试在另一个地方执行相同的操作时,会发生异常。我已经跟踪了所有代码,直到我指出导致问题的陈述。

在类的第一个声明gridview

GridView gridView; 

在OnCreate方法中获得了引用

gridView = (GridView) findViewById(R.id.gridView1);

创建adpater

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:线程退出   未捕获的异常(组= 0x40015560)04-27 10:24:04.586:   E / AndroidRuntime(7823):致命异常:AsyncTask#1 04-27   10:24:04.586:E / AndroidRuntime(7823):java.lang.RuntimeException:An   执行doInBackground时发生错误()04-27 10:24:04.586:   E / AndroidRuntime(7823):at   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):at   java.util.concurrent.FutureTask.setException(FutureTask.java:125)   04-27 10:24:04.586:E / AndroidRuntime(7823):at   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):at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)   04-27 10:24:04.586:E / AndroidRuntime(7823):at   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:581)   04-27 10:24:04.586:E / AndroidRuntime(7823):at   java.lang.Thread.run(Thread.java:1019)04-27 10:24:04.586:   E / AndroidRuntime(7823):引起:   java.lang.UnsupportedOperationException:removeAllViews()不是   AdapterView 04-27 10:24:04.586:E / AndroidRuntime(7823)支持:     在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):at   com.etaworx.thesudokusolver.MainActivity $ solveTask.doInBackground(MainActivity.java:1)   04-27 10:24:04.586:E / AndroidRuntime(7823):at   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):活动   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):at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)   04-27 10:24:09.849:E / WindowManager(7823):at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)   04-27 10:24:09.849:E / WindowManager(7823):at   android.view.Window $ LocalWindowManager.addView(Window.java:424)04-27   10:24:09.849:E / WindowManager(7823):at   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):at   android.view.View.performClick(View.java:2485)04-27 10:24:09.849:   E / WindowManager(7823):at   android.view.View $ PerformClick.run(View.java:9080)04-27 10:24:09.849:   E / WindowManager(7823):at   android.os.Handler.handleCallback(Handler.java:587)04-27   10:24:09.849:E / WindowManager(7823):at   android.os.Handler.dispatchMessage(Handler.java:92)04-27   10:24:09.849:E / WindowManager(7823):at   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.Method.invokeNative(Native Method)04-27   10:24:09.849:E / WindowManager(7823):at   java.lang.reflect.Method.invoke(Method.java:507)04-27 10:24:09.849:   E / WindowManager(7823):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)   04-27 10:24:09.849:E / WindowManager(7823):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)04-27   10:24:09.849:E / WindowManager(7823):at   dalvik.system.NativeStart.main(原生方法)

2 个答案:

答案 0 :(得分:1)

我认为solve();函数在主线程上运行,doinbackground()在后台线程上运行。像这样改变你的solve()函数

@Override
protected String doInBackground(Object... parametros) {
   this.runOnUiThread(new Runnable() {
      public void run() {
         solve();
      }
   });
   return null; 
}

答案 1 :(得分:1)

的AsyncTask

  • onPreExecute - 在UI线程上调用,显示progressDialog here
  • doInBackground() - 在onPreExecute之后立即调用。通常启动新线程并在此处执行长时间操作,如web hit
  • onPostExecute() - 在UI线程中执行后台线程后立即调用,在此处关闭progressDialog

所以避免在doInBackground()中的UI线程上执行。 Link

您的日志在AdatpterView上的removeAllViews上显示异常请检查此异常,以下链接可能对您有用

Link1Link2