-2

我在使用 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();
    }

}

}
4

2 回答 2

1

这条线可能会导致问题。

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

请确保 loja 不为空。

于 2013-05-28T22:04:04.510 回答
0

看来您的 doInBackground 正在访问一个空变量。您应该进行调试以查看女巫变量为空。

于 2013-05-28T22:03:30.230 回答