0

I am using a Gridview to show some TextViews in the inside. I have created a clear button that set a new Adapter with empty ArrayList and it works fine. When I tried to perform the same operation in another place, an exception happen. I have tracked all my code till I have pointed out the statement that cause the problem.

Declairing the gridview at the first of the class

GridView gridView; 

Got a reference in OnCreate method

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

Creating the 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

What I am doing in Clear() method and works fine:

gridView.setAdapter(new ArrayAdapter<String>(MainActivity.this,R.layout.list_item,listEmpty));

What I am doing to set the values and DOES NOT WORK:

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

Some of the Log:

04-27 10:24:04.556: W/dalvikvm(7823): threadid=9: thread exiting with uncaught exception (group=0x40015560) 04-27 10:24:04.586: E/AndroidRuntime(7823): FATAL EXCEPTION: AsyncTask #1 04-27 10:24:04.586: E/AndroidRuntime(7823): java.lang.RuntimeException: An error occured while executing 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): Caused by: java.lang.UnsupportedOperationException: removeAllViews() is not supported in AdapterView 04-27 10:24:04.586: 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): 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 more 04-27 10:24:09.849: E/WindowManager(7823): Activity com.etaworx.thesudokusolver.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054e258 that was originally added here 04-27 10:24:09.849: E/WindowManager(7823): android.view.WindowLeaked: Activity com.etaworx.thesudokusolver.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054e258 that was originally added here 04-27 10:24:09.849: E/WindowManager(7823): at 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(Native Method)

Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66

2 Answers2

1

I think solve(); function running on main thread, doinbackground() running on background thread. change your solve() function like this

@Override
protected String doInBackground(Object... parametros) {
   this.runOnUiThread(new Runnable() {
      public void run() {
         solve();
      }
   });
   return null; 
}
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
1

AsyncTask

  • onPreExecute - invoked on UI thread , show progressDialog here
  • doInBackground() - invoked immediately after onPreExecute. Normally starts new thread and perform the long time operations here like web hit
  • onPostExecute() - invoked immediately after background thread execution in UI thread, dismiss progressDialog here

so avoid performing on UI thread in doInBackground(). Link

Your logs showing that exception on removeAllViews on AdatpterView please check for this exception and the below links may useful for you

Link1, Link2

Community
  • 1
  • 1
Siva
  • 71
  • 5