报警接收器显示空指针异常

时间:2014-03-29 12:08:50

标签: android android-asynctask alarmmanager

在我的应用程序中,我正在调用来自mainactivity的警报广播。警报接收并显示写入为onreceive的第一行的吐司。但是onreceive方法中包含的asynctask显示以下错误。请帮助。

AlarmReceiver.java

package com.androidhive.pushnotifications;

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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.androidhive.pushnotifications.UserFunctions;


public class AlarmReceiver extends BroadcastReceiver {
    private static String KEY_SUCCESS = "success";
    String id1 = "15";
    //UserFunctions userFunctions;
    DatabaseHandler1 db1;
    AsyncTask<Void, Void, Void> mRegisterTask;


     @Override
     public void onReceive(Context arg0, Intent arg1) {
         Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

         mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {

      UserFunctions userFunction = new UserFunctions();
        Log.d("Button", "Logout");
JSONObject json1 = userFunction.logoutUser1(id1);

try {
    if (json1.getString(KEY_SUCCESS) != null) {
        String res4 = json1.getString(KEY_SUCCESS); 
        if(Integer.parseInt(res4) == 3){


             db1.resetTables();

        }

        else{
            // Error in login
            //loginErrorMsg.setText("error");
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}
return null;
}

    @Override
    protected void onPostExecute(Void result) {
                    mRegisterTask = null;
    }

    };
mRegisterTask.execute();        

}
}

logcat的

03-29 07:33:04.595: E/AndroidRuntime(14116): FATAL EXCEPTION: AsyncTask #1
03-29 07:33:04.595: E/AndroidRuntime(14116): java.lang.RuntimeException: An error occured while executing doInBackground()
03-29 07:33:04.595: E/AndroidRuntime(14116):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.lang.Thread.run(Thread.java:841)
03-29 07:33:04.595: E/AndroidRuntime(14116): Caused by: java.lang.NullPointerException
03-29 07:33:04.595: E/AndroidRuntime(14116):    at com.androidhive.pushnotifications.AlarmReceiver$1.doInBackground(AlarmReceiver.java:46)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at com.androidhive.pushnotifications.AlarmReceiver$1.doInBackground(AlarmReceiver.java:1)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-29 07:33:04.595: E/AndroidRuntime(14116):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-29 07:33:04.595: E/AndroidRuntime(14116):    ... 4 more

2 个答案:

答案 0 :(得分:0)

试试这个..

您尚未初始化DatabaseHandler1 id1 ,请将其初始化为

db1 = new DatabaseHandler1();

试。

所以这是null然后你在下面的线使用那么你的NPE

JSONObject json1 = userFunction.logoutUser1(id1);

答案 1 :(得分:0)

我认为问题出在这里。您刚刚声明了数据库类

 DatabaseHandler1 db1;

未初始化,因此您遇到了问题。所以在onRecieve()方法中初始化它。

 db1 = new DatabaseHandler1();