2011-08-08 4 views
5

Clickイベントの進捗ダイアログをListViewに入れようとしていますが、次のコードで説明したようにエラーが発生しました。 "WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?"

コード

final ListView lv1 = (ListView) findViewById(R.id.list); 
    lv1.setAdapter(new EfficientAdapter(this)); 

    lv1.setTextFilterEnabled(true); 

    lv1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> a, View v, 
       final int position, long id) { 
      final ProgressDialog pd = ProgressDialog.show(Add_Entry.this, 
        "", "Please Wait...."); 
      new Thread() { 
       public void run() { 

        if (lv1.getItemAtPosition(position).equals(0)) { 

         Intent edit = new Intent(getApplicationContext(), 
           SourceOfStress.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity("SorceOfStress", 
           edit); 

        } 
        if (lv1.getItemAtPosition(position).equals(1)) { 
         Intent edit = new Intent(getParent(), 
           SourceOFSymptoms.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity(
           "SourceOFSymptoms", edit); 
        } 
        if (lv1.getItemAtPosition(position).equals(2)) { 
         Intent edit = new Intent(getParent(), 
           Stress_Resilliance.class); 
         TabGroupActivity parentActivity = (TabGroupActivity) getParent(); 
         edit.putExtra("currActi", "AddEntry"); 
         parentActivity.startChildActivity(
           "Stress_Resilliance", edit); 
        } 
        pd.dismiss(); 
       } 
      }.start(); 
     } 

    }); 

私のファイル名がAdd_Entry.java であり、エラーがのため、これは主に発生し、この

final ProgressDialog pd = new ProgressDialog(Add_Entry.this).show(Add_Entry.this,"","Please wait...", true); 

答えて

4

これを避けるために、あなたのコードを交換し、これと

ProgressDialog.show(Add_Entry.this, "", "Please Wait...."); 

を、してみてください。あなたはそれをすることはできません。

Handler mechanismを使用してUIコンポーネントを更新します。

ここからハンドラクラスを使用して、バックグラウンドスレッドでProgressBarビューを更新します。

package de.vogella.android.handler; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

public class ProgressTestActivity extends Activity { 
    private Handler handler; 
    private ProgressBar progress; 
    private TextView text; 


/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    progress = (ProgressBar) findViewById(R.id.progressBar1); 
    text = (TextView) findViewById(R.id.textView1); 

    } 

    public void startProgress(View view) { 
    // Do something long 
    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
     for (int i = 0; i <= 10; i++) { 
      final int value = i; 
      try { 
      Thread.sleep(2000); 
      } catch (InterruptedException e) { 
      e.printStackTrace(); 
      } 
      progress.post(new Runnable() { 
      @Override 
      public void run() { 
       text.setText("Updating"); 
       progress.setProgress(value); 
      } 
      }); 
     } 
     } 
    }; 
    new Thread(runnable).start(); 
    } 

} 
+0

私は記事のこの部分がなくなったと思います。 –

+0

@Brias私はそれを修正しました。彼のウェブサイトを再編成したようだ。 – Reno

0

ラインで来ます悪い文脈参照。あなたがスレッドからUIを更新しようとしている

ProgressDialog.show(v.getRootView().getContext(), "", "Please Wait...."); 
+0

PDがどのように我々はPDに値を再割り当てすることができますよりも、最終的として定義されているまあRASEL?(私の知識あたりなど) –

+0

Oh.really?あなたが気にする必要がありabove.Thingのように使用してください使用してProgressDialogオブジェクトを作成しています新しい – Rasel

+1

@ Jignesh Ansodariyaあなたが間違ったコンテキストを使用しているので、適切なコンテキストを使用してみてください。 –

3
WindowManager$BadTokenException 

のように使用します

ProgressDialog.show(Add_Entry.this, 
        "", "Please Wait...."); 
+0

これを試してみましたが、「原因:java.lang.RuntimeException:Looper.prepare() 」という行の中にハンドラを作成できませんでした。parentActivity.startChildActivity( "SorceOfStress"、 \t \t \t \t \t \t \t \t \t編集); –

+0

これは、ハンドラを使用していないことを意味します。私の答えはこちらを参照してください.http://stackoverflow.com/questions/6894698/rotating-wheel-progress-dialog-while-deleting-folder-from-sd-card/6894744#6894744 –