将字符串从活动传递到服务时出现NullPointerException

时间:2015-03-22 20:30:34

标签: java android

我将两个字符串从NewsFeed.java传递给CheckService.java

在NewsFeed.java中,我通过这样的包传递字符串:

                        basket.putString("KEY1", id[i]);
                        basket.putString("KEY2", message[i]);
                        basket.putString("KEY3", timeFormat[i]);
                        passNotiData = new Intent(getActivity(),
                                CheckService.class);
                        passNotiData.putExtras(basket);            

                       getActivity().startService(passNotiData);

NewsFeed.java是一个片段。此代码位于doInBackground的{​​{1}}方法中。 在CheckService.java中:

AsynkTask

我在 Bundle gotBasket =intent.getExtras(); oldId=gotBasket.getString("KEY1"); oldTime=gotBasket.getString("KEY3"); 收到错误oldId=gotBasket.getString("KEY1");。我已将NULLPOINTEREXCEPTION初始化为oldId

logcat的:

String oldId= new String();

BootBroadcast.java

  03-23 02:04:25.299: E/AndroidRuntime(16212): FATAL EXCEPTION: IntentService[CheckService]
03-23 02:04:25.299: E/AndroidRuntime(16212): java.lang.NullPointerException
03-23 02:04:25.299: E/AndroidRuntime(16212):    at com.appquest.awaaziitkgp.notification.CheckService.doWakefulWork(CheckService.java:70)
03-23 02:04:25.299: E/AndroidRuntime(16212):    at com.appquest.awaaziitkgp.notification.WakefulIntentService.onHandleIntent(WakefulIntentService.java:39)
03-23 02:04:25.299: E/AndroidRuntime(16212):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
03-23 02:04:25.299: E/AndroidRuntime(16212):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 02:04:25.299: E/AndroidRuntime(16212):    at android.os.Looper.loop(Looper.java:137)
03-23 02:04:25.299: E/AndroidRuntime(16212):    at android.os.HandlerThread.run(HandlerThread.java:60)  

AlarmReceiver.java

    package com.appquest.awaaziitkgp.notification;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class BootBroadcast extends BroadcastReceiver {
    private static final int PERIOD = 60000; // 1 minutes

    @Override
    public void onReceive(Context context, Intent intent) {
        AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent myIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, myIntent, 0);
        // context.startService(myIntent);

        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 60000, PERIOD, pi);
    }

}

WakefulIntentService.java

    package com.appquest.awaaziitkgp.notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        WakefulIntentService.acquireStaticLock(context);

        context.startService(new Intent(context, CheckService.class));
    }

}

CheckService.java

package com.appquest.awaaziitkgp.notification;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

abstract public class WakefulIntentService extends IntentService {

    abstract void doWakefulWork(Intent intent);

    public static final String LOCK_NAME_STATIC = "com.appquest.ServiceHandler.Static";
    private static PowerManager.WakeLock lockStatic = null;

    public static void acquireStaticLock(Context context) {
        getLock(context).acquire();
    }

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager mgr = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);

            lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    LOCK_NAME_STATIC);
            lockStatic.setReferenceCounted(true);
        }

        return (lockStatic);
    }

    public WakefulIntentService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            doWakefulWork(intent);
        } finally {
            if (getLock(this).isHeld())
                getLock(this).release();

        }
    }

}

1 个答案:

答案 0 :(得分:0)

问题是gotBasket变量为空。很难从代码片段中看出,但CheckService中的intent变量指向什么?它是否可能指向CheckService的不同实例?这可以解释为什么捆绑丢失。