不调用AsyncTask doInBackGround

时间:2016-06-15 15:05:43

标签: android android-asynctask

我为我的应用创建了一个AsyncTask,但是在onpreexecute上没有调用doinbackground方法。这是我的代码:

arg

以下是我的称呼方式:

public class NewProductActivity extends AsyncTask<String,Void,Boolean>{

@Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = ProgressDialog.show(context, "Please wait", "Sending data...", true);

    }


    @Override
    protected Boolean doInBackground(String... params) {
        try {
            boolean added = dbo.addDataToDB(name, pass);

            Log.d("1234rrr", name +" "+pass);

            return added;
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result){
            Toast.makeText(context, "successfully added.", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context, "failed to add data.", Toast.LENGTH_SHORT).show();
        }

        progressDialog.dismiss();
    }
}

但该应用程序仍停留在onpreexecute上。顺便说一句,我只有一个asynctask。任何人都可以帮我解决这个问题吗?谢谢!

编辑:

我已经意识到,当我将日志消息放在doinbackground的第一行时,我收到了消息。我猜测问题在于addDataToDB函数。以下是addDataToDB的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new NewProductActivity(UserName.getText().toString(), Password.getText().toString(), this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        else
            new NewProductActivity(UserName.getText().toString(),Password.getText().toString(),this).execute();

0 个答案:

没有答案