使用异步任务制作进度对话框

Posted

技术标签:

【中文标题】使用异步任务制作进度对话框【英文标题】:making a ProgressDialog with Asynch task 【发布时间】:2012-04-17 09:12:48 【问题描述】:

我正在尝试在异步任务中获取我的代码,只有我的应用程序不断崩溃,有人可以找出导致此错误的原因(我认为 NullPointerException)。老实说,我只是从另一个问题中复制了一些代码并将所有内容都放入其中。

package net.thinkbin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class entertainment extends ListActivity 

private static final String TITLE = "Title";
private static final String AUTHOR = "Author";
private static final String VIEWS = "Views";
private static final String RATES = "Rates";
private static final String CONTENT = "Content";

JSONArray ideas = null;

@Override
protected void onCreate(Bundle savedInstanceState) 
// TODO Auto-generated method stub


super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder3);
new DoInBackgrd().execute();


Button view = (Button) findViewById(R.id.button1);
view.setOnClickListener(new View.OnClickListener() 

    public void onClick(View v) 
        // TODO Auto-generated method stub
        startActivity(new Intent("net.thinkbin.TUTORIAL1"));
        overridePendingTransition(0, 0);
        finish();
    
);

Button share = (Button) findViewById(R.id.button2);
share.setOnClickListener(new View.OnClickListener() 

    public void onClick(View v) 
        // TODO Auto-generated method stub
        startActivity(new Intent("net.thinkbin.SHARE"));
        overridePendingTransition(0, 0);
        finish();
    
);

Button menu = (Button) findViewById(R.id.buttonhome);
menu.setOnClickListener(new View.OnClickListener() 

    public void onClick(View v) 
        // TODO Auto-generated method stub
        startActivity(new Intent("net.thinkbin.MENU"));
        overridePendingTransition(0, 0);
        finish();
    

);



         

private class DoInBackgrd extends AsyncTask<Void, Void, Void> implements
DialogInterface.OnCancelListener 

private ProgressDialog processDialog;

@Override
protected void onPreExecute() 
ProgressDialog.show(entertainment.this, "", "Loading...");  
processDialog.setCancelable(true);


public void onCancel(DialogInterface arg0) 
// TODO Auto-generated method stub

if (processDialog.isShowing()) 
processDialog.dismiss();





@Override
protected Void doInBackground(Void... arg0) 
// TODO Auto-generated method stub

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String,      String>>();


JSONObject json =     JSONfunctions.getJSONfromURL("http://www.thinkbin.net/include/api/index.php?cat=Entertainment&type=Views&i=10");

try

        ideas = json.getJSONArray("Ideas");

        // looping through All Contacts
    for(int i = 0; i < ideas.length(); i++)
        JSONObject c = ideas.getJSONObject(i);

        // Storing each json item in variable
        String title = c.getString(TITLE);
        String author = c.getString(AUTHOR);
        String views = c.getString(VIEWS);
        String rates = c.getString(RATES);
        String content = c.getString(CONTENT);

        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
        map.put(TITLE, "Title: " + title);
        map.put(AUTHOR, "Author: " + author);
        map.put(VIEWS, "Views: " + views);
        map.put(RATES, "Rates: " + rates);
        map.put(CONTENT, content);

        // adding HashList to ArrayList
        mylist.add(map);


       


catch(JSONException e)        
     Log.e("log_tag", "Error parsing data "+e.toString());
   

ListAdapter adapter = new SimpleAdapter(entertainment.this, mylist ,    R.layout.main2, 
        new String[]  TITLE, AUTHOR, VIEWS, RATES, CONTENT , 
        new int[]  R.id.item_title, R.id.item_subtitle, R.id.item3, R.id.item4, R.id.item5 );

setListAdapter(adapter);

//selecting single ListView item
ListView lv = getListView();

//Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() 

public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) 
// getting values from selected ListItem
String Title2 = ((TextView) view.findViewById(R.id.item_title)).getText().toString();
String Author2 = ((TextView)  view.findViewById(R.id.item_subtitle)).getText().toString();
String Content2 = ((TextView) view.findViewById(R.id.item5)).getText().toString();

Intent in = new Intent(getApplicationContext(), idea.class);
overridePendingTransition(0, 0);
in.putExtra(TITLE, Title2);
in.putExtra(AUTHOR, Author2);
in.putExtra(CONTENT, Content2);


startActivity(in);



);
return null;


@Override
protected void onPostExecute(Void unused) 
if (processDialog.isShowing()) 
    processDialog.dismiss();


    




日志猫:

04-03 15:56:43.988: D/dalvikvm(324): GC_EXTERNAL_ALLOC freed 48K, 53% free 2552K/5379K, external 1625K/2137K, paused 189ms
04-03 15:56:48.768: D/dalvikvm(324): GC_EXTERNAL_ALLOC freed 7K, 53% free 2570K/5379K, external 3086K/3528K, paused 33ms
04-03 15:56:48.878: D/dalvikvm(324): GC_EXTERNAL_ALLOC freed 1K, 53% free 2571K/5379K, external 4286K/5352K, paused 39ms
04-03 15:59:07.597: D/dalvikvm(324): GC_CONCURRENT freed 958K, 53% free 3162K/6727K, external 5965K/6851K, paused 3ms+4ms
04-03 16:51:19.427: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 49K, 53% free 2552K/5379K, external 1625K/2137K, paused 48ms
04-03 16:51:24.338: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 7K, 53% free 2571K/5379K, external 3086K/3528K, paused 40ms
04-03 16:51:24.448: D/dalvikvm(362): GC_EXTERNAL_ALLOC freed 1K, 53% free 2572K/5379K, external 4286K/5352K, paused 41ms
04-03 16:51:28.468: D/AndroidRuntime(362): Shutting down VM
04-03 16:51:28.468: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-03 16:51:28.478: E/AndroidRuntime(362): FATAL EXCEPTION: main
04-03 16:51:28.478: E/AndroidRuntime(362): java.lang.RuntimeException: Unable to start activity ComponentInfonet.thinkbin/net.thinkbin.entertainment: java.lang.NullPointerException
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.os.Looper.loop(Looper.java:123)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 16:51:28.478: E/AndroidRuntime(362):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:51:28.478: E/AndroidRuntime(362):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 16:51:28.478: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 16:51:28.478: E/AndroidRuntime(362):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 16:51:28.478: E/AndroidRuntime(362):  at dalvik.system.NativeStart.main(Native Method)
04-03 16:51:28.478: E/AndroidRuntime(362): Caused by: java.lang.NullPointerException
04-03 16:51:28.478: E/AndroidRuntime(362):  at net.thinkbin.entertainment$DoInBackgrd.onPreExecute(entertainment.java:92)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.os.AsyncTask.execute(AsyncTask.java:391)
04-03 16:51:28.478: E/AndroidRuntime(362):  at net.thinkbin.entertainment.onCreate(entertainment.java:43)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 16:51:28.478: E/AndroidRuntime(362):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-03 16:51:28.478: E/AndroidRuntime(362):  ... 11 more
04-03 16:52:48.988: D/dalvikvm(397): GC_EXTERNAL_ALLOC freed 50K, 53% free 2552K/5379K, external 1625K/2137K, paused 134ms
04-03 16:52:53.798: D/dalvikvm(397): GC_EXTERNAL_ALLOC freed 7K, 53% free 2571K/5379K, external 3086K/3528K, paused 35ms
04-03 16:52:53.909: D/dalvikvm(397): GC_EXTERNAL_ALLOC freed 1K, 53% free 2572K/5379K, external 4286K/5352K, paused 42ms
04-03 16:52:57.827: D/AndroidRuntime(397): Shutting down VM
04-03 16:52:57.827: W/dalvikvm(397): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-03 16:52:57.837: E/AndroidRuntime(397): FATAL EXCEPTION: main
04-03 16:52:57.837: E/AndroidRuntime(397): java.lang.RuntimeException: Unable to start activity ComponentInfonet.thinkbin/net.thinkbin.entertainment: java.lang.NullPointerException
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.os.Looper.loop(Looper.java:123)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 16:52:57.837: E/AndroidRuntime(397):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 16:52:57.837: E/AndroidRuntime(397):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 16:52:57.837: E/AndroidRuntime(397):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 16:52:57.837: E/AndroidRuntime(397):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 16:52:57.837: E/AndroidRuntime(397):  at dalvik.system.NativeStart.main(Native Method)
04-03 16:52:57.837: E/AndroidRuntime(397): Caused by: java.lang.NullPointerException
04-03 16:52:57.837: E/AndroidRuntime(397):  at net.thinkbin.entertainment$DoInBackgrd.onPreExecute(entertainment.java:92)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.os.AsyncTask.execute(AsyncTask.java:391)
04-03 16:52:57.837: E/AndroidRuntime(397):  at net.thinkbin.entertainment.onCreate(entertainment.java:43)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-03 16:52:57.837: E/AndroidRuntime(397):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-03 16:52:57.837: E/AndroidRuntime(397):  ... 11 more
04-03 16:52:59.357: I/Process(397): Sending signal. PID: 397 SIG: 9
04-03 17:08:38.488: D/dalvikvm(434): GC_EXTERNAL_ALLOC freed 51K, 53% free 2552K/5379K, external 1625K/2137K, paused 78ms
04-03 17:08:43.277: D/dalvikvm(434): GC_EXTERNAL_ALLOC freed 7K, 53% free 2571K/5379K, external 3086K/3528K, paused 34ms
04-03 17:08:43.397: D/dalvikvm(434): GC_EXTERNAL_ALLOC freed 1K, 53% free 2572K/5379K, external 4286K/5352K, paused 41ms
04-03 17:08:48.317: D/dalvikvm(434): GC_CONCURRENT freed 959K, 53% free 3163K/6727K, external 5965K/6851K, paused 4ms+4ms
04-03 17:09:00.288: D/dalvikvm(434): GC_CONCURRENT freed 1260K, 50% free 3974K/7879K, external 4533K/5704K, paused 4ms+7ms

【问题讨论】:

当错误出现时总是在这里发布完整的 logcat... entertainment.java see line no.92,这是? processDialog.setCancelable(true); 看我的答案你忘了分配... 【参考方案1】:
@Override
protected void onPreExecute() 

processDialog = ProgressDialog.show(entertainment.this, "", "Loading...");  
processDialog.setCancelable(true);


【讨论】:

【参考方案2】:

processDialog 为空,因为它尚未在您的 preexecute 方法中正确初始化...

【讨论】:

以上是关于使用异步任务制作进度对话框的主要内容,如果未能解决你的问题,请参考以下文章

在异步任务中给出异常的进度对话框[重复]

如果异步任务被解除,则重新显示进度对话框

异步任务的进度对话框

如何关闭带有异步任务的进度对话框并避免“您的活动是不是运行错误?”

我如何在我的异步任务类中使用圆形旋转进度条

尝试连接到服务器时带有异步任务的进度条