asyncTask出错

时间:2013-05-28 21:57:22

标签: android android-asynctask

我遇到了AsynTask的问题,不知道该怎么做...... 我和其他班级做了同样的事情,似乎工作正常!但是这个,我得到一个错误,我不明白.....

这是LogCat:

05-28 22:48:09.271: W/System.err(28530): java.util.concurrent.ExecutionException: java.lang.NullPointerException
05-28 22:48:09.271: W/System.err(28530):    at java.util.concurrent.FutureTask.report(FutureTask.java:94)
05-28 22:48:09.271: W/dalvikvm(28530): threadid=11: thread exiting with uncaught exception (group=0x41f6dac8)
05-28 22:48:09.271: W/System.err(28530):    at java.util.concurrent.FutureTask.get(FutureTask.java:160)
05-28 22:48:09.271: W/System.err(28530):    at android.os.AsyncTask.get(AsyncTask.java:482)
05-28 22:48:09.271: W/System.err(28530):    at com.example.basicmaponline.Entidade.onCreate(Entidade.java:46)
05-28 22:48:09.271: W/System.err(28530):    at android.app.Activity.performCreate(Activity.java:5250)
05-28 22:48:09.271: W/System.err(28530):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-28 22:48:09.271: W/System.err(28530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
05-28 22:48:09.271: W/System.err(28530):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
05-28 22:48:09.271: W/System.err(28530):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
05-28 22:48:09.271: W/System.err(28530):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
05-28 22:48:09.271: W/System.err(28530):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 22:48:09.271: W/System.err(28530):    at android.os.Looper.loop(Looper.java:137)
05-28 22:48:09.271: W/System.err(28530):    at android.app.ActivityThread.main(ActivityThread.java:5293)
05-28 22:48:09.271: W/System.err(28530):    at java.lang.reflect.Method.invokeNative(Native Method)
05-28 22:48:09.271: W/System.err(28530):    at java.lang.reflect.Method.invoke(Method.java:511)
05-28 22:48:09.271: W/System.err(28530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-28 22:48:09.271: W/System.err(28530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-28 22:48:09.271: W/System.err(28530):    at dalvik.system.NativeStart.main(Native Method)
05-28 22:48:09.271: W/System.err(28530): Caused by: java.lang.NullPointerException
05-28 22:48:09.271: W/System.err(28530):    at com.mlab.android.basicoverlays.PostgreSQL.getEntidade(PostgreSQL.java:114)
05-28 22:48:09.271: W/System.err(28530):    at com.example.basicmaponline.Entidade$loadDatabase.doInBackground(Entidade.java:175)
05-28 22:48:09.271: W/System.err(28530):    at com.example.basicmaponline.Entidade$loadDatabase.doInBackground(Entidade.java:1)
05-28 22:48:09.271: W/System.err(28530):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-28 22:48:09.271: W/System.err(28530):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-28 22:48:09.271: W/System.err(28530):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-28 22:48:09.271: W/System.err(28530):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-28 22:48:09.271: W/System.err(28530):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-28 22:48:09.271: W/System.err(28530):    at java.lang.Thread.run(Thread.java:856)
05-28 22:48:09.281: E/AndroidRuntime(28530): FATAL EXCEPTION: AsyncTask #1
05-28 22:48:09.281: E/AndroidRuntime(28530): java.lang.RuntimeException: An error occured while executing doInBackground()
05-28 22:48:09.281: E/AndroidRuntime(28530):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.lang.Thread.run(Thread.java:856)
05-28 22:48:09.281: E/AndroidRuntime(28530): Caused by: java.lang.NullPointerException
05-28 22:48:09.281: E/AndroidRuntime(28530):    at com.mlab.android.basicoverlays.PostgreSQL.getEntidade(PostgreSQL.java:114)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at com.example.basicmaponline.Entidade$loadDatabase.doInBackground(Entidade.java:175)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at com.example.basicmaponline.Entidade$loadDatabase.doInBackground(Entidade.java:1)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-28 22:48:09.281: E/AndroidRuntime(28530):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-28 22:48:09.281: E/AndroidRuntime(28530):    ... 4 more

这是我的代码,错误来了......

package com.example.basicmaponline;

import java.sql.SQLException;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.mlab.android.basicoverlays.PostgreSQL;
import com.mlab.android.basicoverlays.SQLbalcao;
import com.mlab.android.basicoverlays.SQLloja;

public class Balcoes extends ListActivity{

ArrayList<SQLbalcao> listaBalcao;
String[] lista;
SQLloja loja;

ProgressDialog progressDialog;

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

    //Action Bar is the bar on the top of each activity !
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();
    loja = (SQLloja) intent.getSerializableExtra("loja");

    if(listaBalcao == null){
        new loadDatabase().execute();
    }

    /*lista = new String[listaBalcao.size()];
    int i = 0;
    for(SQLbalcao balcao : listaBalcao){
        lista[i++] = balcao.getNome_entidade();
    }

    setListAdapter(new ArrayAdapter<String>(Balcoes.this, android.R.layout.simple_list_item_1,lista));*/
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Intent ourIntent = new Intent("com.example.basicmaponline.ENTIDADE");
    ourIntent.putExtra("id_entidade", listaBalcao.get(position).getId_entidade());
    ourIntent.putExtra("id_loja", listaBalcao.get(position).getId_loja_cidadao());
    startActivity(ourIntent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    //return super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.homeactionbar, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==R.id.lojasHome){
        Intent intent = new Intent("com.example.basicmaponline.MENU");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    else {
        super.onBackPressed();
    }
    return true;
}

public class loadDatabase extends AsyncTask<Void, Void, ArrayList<SQLbalcao>>{

    @Override
    protected void onPreExecute()
    {
        progressDialog = new ProgressDialog(Balcoes.this); 
        progressDialog.setTitle("Processando...");
        progressDialog.setMessage("Por favor,espera...");
        progressDialog.setCancelable(true);
        //progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        //progressDialog.setMax(100);
        progressDialog.show();             
    }; 

    @Override
    protected ArrayList<SQLbalcao> doInBackground(Void... params) { 

        ArrayList<SQLbalcao> listaBalcoes = new ArrayList<SQLbalcao>();

        try {
            PostgreSQL pSQL = new PostgreSQL();
            listaBalcoes = pSQL.getBalcoesByOrder(pSQL.getBalcoes(loja.getId()));
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return listaBalcoes;

    }

    @Override
    protected void onPostExecute(ArrayList<SQLbalcao> listaBalcoes){
        listaBalcao = listaBalcoes;

        lista = new String[listaBalcao.size()];
        int i = 0;
        for(SQLbalcao balcao : listaBalcao){
            lista[i++] = balcao.getNome_entidade();
        }

        setListAdapter(new ArrayAdapter<String>(Balcoes.this, android.R.layout.simple_list_item_1,lista));

        progressDialog.dismiss();
    }

}

}

2 个答案:

答案 0 :(得分:1)

此行可能会导致问题。

listaBalcoes = pSQL.getBalcoesByOrder(pSQL.getBalcoes(loja.getId()));

请确保loja不为空。

答案 1 :(得分:0)

您的doInBackground似乎正在访问一个null变量。你应该做一个调试,看看女巫变量是否为空。