2011-03-01 149 views
2

我有一個asynctask其中的onPostExecute我正在調用一個函數來顯示一個customDialog。從AsyncTask調用自定義對話框

這是我的AsyncTask

private class DBTask extends AsyncTask<Long, Boolean, Integer>{ 
ProgressDialog ServerPD = new ProgressDialog(MRRankingActivity.this); 

@Override 
protected void onPreExecute() 
{ 
    ServerPD = ProgressDialog.show (MRRankingActivity.this, "", "Connecting to server...", true, false); 
}//End of onPreExecute 

@Override 
protected Integer doInBackground(Long... params) 
{ 
    int isSuccess=0; 
    publishProgress(isOnline()); 

     if(isOnline()) 
     { 

     getDBData(); 
       if(isOK) 
        { 
        isSuccess=1; 
        } 
     } 
    } 

    return isSuccess; 
} 

    @Override 
protected void onProgressUpdate(Boolean... isConnection) { 
// TODO Auto-generated method stub 

super.onProgressUpdate(isConnection); 
if(isOnline()) 
     { 
     ServerPD.setMessage("Retreving Data"); 
     } 
} 

@Override 
protected void onPostExecute(Integer result) { 
    if (ServerPD.isShowing()) 
    { 
     ServerPD.dismiss(); 
    } 
    if(result==1) 
     { 
      customDialog(); 
     } 

}//End of onPostExecute 

}//End of DBTask 

這是我customDialog功能

public void customDialog(){ 
    Dialog dialog=new Dialog(MRRankingActivity.this); 
    dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.string.app_name); 
    dialog.setContentView(R.layout.result_dialog); 
    final ListView ResultView = (ListView) findViewById(R.id.ListResult); 

    result_Adapter=new ArrayAdapter<String>(MRRankingActivity.this,android.R.layout.simple_list_item_1,result_Array); 

    //Bind Array Adapter to ListView 
    ResultView.setAdapter(result_Adapter); 
    dialog.show(); 

}//end of custom dialog function 

這是我result_dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:orientation="vertical"> 

      <ListView android:id="@+id/ListResult" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent"/> 

     </LinearLayout> 

</RelativeLayout> 

現在,當我運行這段代碼我得到此錯誤

FATAL EXCEPTION: main 
java.lang.NullPointerException 

at com.lalsoft.mobileranking.MRRankingActivity.customDialog(MRRankingActivity.java) 
at com.lalsoft.mobileranking.MRRankingActivity$DBTask.onPostExecute(MRRankingActivity.java)  at com.lalsoft.mobileranking.MRRankingActivity$DBTask.onPostExecute(MRRankingActivity.java) 
at android.os.AsyncTask.finish(AsyncTask.java) 
at android.os.AsyncTask.access$300(AsyncTask.java) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java) 
at android.os.Handler.dispatchMessage(Handler.java) 
at android.os.Looper.loop(Looper.java) 
at android.app.ActivityThread.main(ActivityThread.java) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java) 

如果我在customDialog功能評論

//ResultView.setAdapter(result_Adapter); 

,該customDialog將顯示,沒有列表視圖。當我將適配器設置爲ListView時,會出現此錯誤。

如何解決這個問題??應該做什麼?請幫助

回答

2

替換此行最終ListView ResultView =(ListView)dialog.findViewById(R.id.ListResult);

而不是最終ListView ResultView =(ListView)findViewById(R.id.ListResult);

+0

發生了什麼..? – 2011-03-01 12:19:43

+0

謝謝..它工作:)。它證實我是一個noob。再次感謝好友 – Shijilal 2011-03-01 12:37:19

+0

最受歡迎.... – 2011-03-01 12:39:58

0

在您的代碼中,您的result_Adapter可能爲空。這就是爲什麼當你將其設置爲ResultView其顯示NullPointerException。

另請檢查result_Array是否包含值。

+0

我檢查了包含值。 – Shijilal 2011-03-01 11:58:03

+0

正如CapDroid提到的那樣,刪除最後一個關鍵字ResultView – Mathew 2011-03-01 12:24:34