2011-12-15 14 views
0

私のアプリケーションでは、PopupDialogの中にListViewを使用しようとしています。これはAsyncTaskの内部で行われます。問題はonPreExecuteonPostExecuteの間にあると思います.....すべてが正しく返されますが、findViewById()を使用した後はlist = nullとなります。私は理由を理解できません...... AsyncTaskとAdapterはどちらも内部クラスです。PopupDialog内のListViewがnullです

protected void onPreExecute() { 
     AlertDialog.Builder alert = new AlertDialog.Builder(CreateNewReport.this); 
     LayoutInflater inflater = LayoutInflater.from(CreateNewReport.this); 
     popupView = inflater.inflate(R.layout.add_expenses, null); 

     alert.setTitle("Add Expenses").setView(popupView); 

     alert.setNeutralButton("Save", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

      } 
     }); 

     alert.show(); 
    } 

    protected void onPostExecute(List<Expense> expenses){ 
     availableExpenses = expenses; 

     // create list adapter for available expenses 
     ArrayAdapter<Expense> adapter = new ArrayAdapter<Expense>(CreateNewReport.this, 
       android.R.layout.simple_list_item_1, expenses); 

     // get a reference to the list 
     final ListView list = (ListView) findViewById(R.id.listViewAvailableExpenses); 

     // set the list adapter 
     list.setAdapter(adapter); 

     // find widgets 
     final ProgressBar progress = (ProgressBar) findViewById(R.id.loading_expenses); 
     final LinearLayout listLayout = (LinearLayout) findViewById(R.id.available_expenses); 
     final LinearLayout progressContainer = (LinearLayout) findViewById(R.id.available_expenses_loading); 

     // change visibility as needed 
     progressContainer.setVisibility(View.INVISIBLE); 
     progress.setVisibility(View.INVISIBLE); 
     listLayout.setVisibility(View.VISIBLE); 
    } 

ポップアップボックスのXML

findViewById()呼び出しは(それがアクティビティインスタンスのメソッドです)活動中にあなたのリストを探していますが、あなたのリストが内部にありますので、
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <ProgressBar 
     android:id="@+id/loading_expenses" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@android:style/Widget.ProgressBar.Large" 
     android:layout_centerInParent="true" 
     /> 
     <ListView 
      android:id="@+id/listViewAvailableExpenses" 
      style="@style/Container" 
      android:cacheColorHint="#00000000" 
      /> 
</RelativeLayout> 

答えて

2

ダイアログ。代わりに、ダイアログへの参照を保存してから、theDialog.findViewById(R.id.ListNote)

//PreExecute 
theDialog = alert.create(); //theDialog is an instance variable of the asynctask 
theDialog.show(); 


//PostExecute 
final ListView list = (ListView) theDialog.findViewById(R.id.ListNote); 
+0

スポットをオンにする必要があります。それはまさに正しいものでした。 – Cody

関連する問題