【问题标题】:Calling custom Dialog from AsyncTask从 AsyncTask 调用自定义对话框
【发布时间】:2011-03-01 11:45:15
【问题描述】:

我有一个异步任务,它的 onPostExecute 我正在调用一个函数来显示一个 customDialog。

这是我的异步任务

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)

如果我评论

 //ResultView.setAdapter(result_Adapter);

在我的 customDialog 函数中,customDialog 将显示,没有 Listview。当我将适配器设置为我的 ListView 时出现此错误。

如何解决这个问题??应该怎么做??请帮忙

【问题讨论】:

    标签: android android-asynctask android-listview customdialog


    【解决方案1】:

    替换此行 final ListView ResultView = (ListView)dialog.findViewById( R.id.ListResult );

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

    【讨论】:

    • 谢谢..它工作:)。它证实我是一个菜鸟。再次感谢朋友
    【解决方案2】:

    在您的代码中,您的result_Adapter 可能为空。这就是为什么当您将其设置为 ResultView 时,它会显示 NullPointerException。

    同时检查result_Array 是否包含值。

    【讨论】:

    • 我检查过..它包含值。
    • 正如 CapDroid 提到的,删除 ResultView 的最终关键字
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2022-01-24
    • 2018-08-30
    相关资源
    最近更新 更多