开发者

sendBroadcast(intent) gives a null pointer exception

This is my second post today as fixing the 1st post lead to another problem which I am really struggling on. I want to use Broadcasts and intent to send data packets back from a service to the UI. I managed to bind and start the service successfully see my other post if you want history and code

The null pointer exception comes on the sendBroadcast() inside the service. The service class does have its constructor re-called after binding the UI to the service. This happens from another class so Context can not be easily used. So I guess the sendBroadcast has no-where to go :( I suspect this is my problem...the re-calling of the Service constructor after the initial binding. I have onDestroy, onPause and onResume covered with binding an unbinding. Any ideas or suggestion would be great or maybe I am just going about this wrong is there another way?

ED开发者_StackOverflowIT The previous problem of binding is now solved and due to me being new to the forums a slight delay in accepting the answer...sorry.

The class diagram is like this (it is ported C#code)

Activity doBind (on Curve.class)---> Activity starts a worker class (not treated as a service) Comm.class with a Handler for some comms--> the Comm starts another worker class --> previous worker class finally calls new Curve.class.

It is this last stage Curve.class where the sendBroadcastReceiver() then throws a nullpointer ref becasue the binder is lost. I tested the broadcastreceiver just with a simple timer cutting out the worker classes in between and it works fine. Problems start when the Curve.class is recalled later further down the hierarchy and the binder gets nulled or "lost".

I removed all references of the binder from Curve except in onBind(). This might not be a good idea. Again the code below does work if used with a simple timer started directly from the UI (no other worker classes).

Some more code here: The service

    public class Curve extends Service
{
    private NewCurvePointEventArgs newpoint = null;
    private static final String TAG = "Curve";

    Intent intent;
    int counter = 0;


    @Override
    public void onCreate() {
        super.onCreate();   

    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        IBinder mBinder = new LocalBinder<Curve>(this);
        return mBinder;
    }
    @Override
    public void onStart(Intent intent, int startId) {

    }
    @Override
    public void onDestroy() {

    }
    public Curve(){

    }
    private void refreshintent(NewCurvePointEventArgs tmp)
    {
        ArrayList<String> thepoint = new ArrayList<String>();
        thepoint.add()//various things added here

         Bundle bundle = new Bundle();
            // add data to bundle
            bundle.putStringArrayList("New_Point", thepoint);

        intent = new Intent();
        intent.setAction(NEWCURVE_POINT);
        intent.putExtra("NEW_POINT", bundle

        sendBroadcast(intent);

    }

The activity has this code now. the doBind() is called after the onCreate of the activity.

        private BroadcastReceiver CurveReceiver  = new BroadcastReceiver(){

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Curve.NEWCURVE_POINT)) {
            displaynewpoint(intent); 

        }
    }
};
private ServiceConnection CurveServiceConncetion = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub

        CurveService = ((LocalBinder<Curve>) service).getService();

    }
    @Override
    public void onServiceDisconnected(ComponentName name) {

        // TODO Auto-generated method stub
        CurveService = null;
    }
};

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");

}

@Override
public synchronized void onPause() {
    super.onPause();
    if(D) Log.e(TAG, "- ON PAUSE -");
}

@Override
public void onStop() {
    super.onStop();
    if(D) Log.e(TAG, "-- ON STOP --");
}

@Override
public void onDestroy() {
    super.onDestroy();

    if(D) Log.e(TAG, "--- ON DESTROY ---");

    unregisterReceiver(CurveReceiver);
    unbindService(CurveServiceConncetion); 
}
public  void doBind(){
    Boolean tmp;
    tmp = bindService(new Intent(this, Curve.class), CurveServiceConncetion, Context.BIND_AUTO_CREATE);//Context.BIND_AUTO_CREATE

    IntentFilter filter = new IntentFilter(Curve.NEWCURVE_POINT);  
    registerReceiver(CurveReceiver, filter);
}

This problem to me is because the Curve.class has its constructor called again after the initial doBind(). Surely there must be a way around this otherwise I have to load my worker classes closer in hierarchy to the UI with the code from Curve.class???

EDIT Curve is an object that processes data, constants etc sent from an external machine and contains the processed data in arrays. The logCat did of course exist I just wasn't looking in the right place here it is

ARN/System.err(10505): java.lang.NullPointerException WARN/System.err(10505): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:271) WARN/System.err(10505): at pi.droid.Core.Curve.refreshintent(Curve.java:206) WARN/System.err(10505): at pi.droid.Core.Curve.AddPoint(Curve.java:400) WARN/System.err(10505): at pi.droid.Core.Comm.CommMeasure$CommMeasurement.AddPoint(CommMeasure.java:363) WARN/System.err(10505): at pi.droid.Core.Comm.CommMeasure$GenericCommMeasurement.TryProcessData(CommMeasure.java:168) WARN/System.err(10505): at pi.droid.Core.Comm.CommMeasure$CommMeasurement.ProcessData(CommMeasure.java:302) WARN/System.err(10505):at pi.droid.Core.Comm.ClientConnection$timer_tick.run(ClientConnection.java:164) WARN/System.err(10505): at java.util.Timer$TimerImpl.run(Timer.java:289)

You can also see the chain of the 2 other worker classes I use. The constructor of Curve is called after the bind from CommMeasure. So this is my problem. Do I need to totally change how my program is set up or is there another way around this?

FINAL EDIT This code is brought from c# and Curve used eventhandlers to pass data around. I got rid of all them(java listeners) and used android Handler and broadcastreceiver. It has been suggested that I should pass the CurveService around but this will be problematic as Curve has multiple constructors. The no parameter 1 for the service and then 1 like this

    public Curve(Unit XUnit, Unit YUnit)
    {           this.Title = "Curve";
        this.finished = false;
        this.XUnit = XUnit;
        this.YUnit = YUnit;
        this.YDirection = CurveDirection.Unspecified;

    }

so surely instantiating that would be a problem with CurveService, which has to be like this: public Curve(){} ?? Anyway many thanks for all your help and advice.

Final Edit +1..lol

The UI creates a new instance of ClientConnection, that in turns creates a new instance of CommMeasure and finally CommMeasure creates a new instance of Curve to access Curve.addpoint. I think this thread and the other linked 1 goes beyond a simple android problem and highlights the difficulties of code porting. Any .Net developer for example reading this will learn some peculiarities of android a lot quicker than I did. There is also good working code in there. Thanks to all who helped especially Justin Breitfeller


The best thing for you to do is follow the example from the Android APIDemos.

A service to be used like you want to use it: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html

Look at the Binding class inside of this file to see a class that does binding like you should: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalServiceActivities.html

Finally, if your constructor is being called twice on your service, you aren't binding to your service properly, or perhaps you are unbinding from it and binding to it again unexpectedly.

EDIT From the stack trace, it appears that CommMeasure needs to have a reference to the instance of Curve that you receive in onServiceConnected.

EDIT 2 If you really want to make your life simple, pass getApplicationContext() to your CommMeasure class and just appContext.sendBroadcast() from that class to send out your point. This will prevent you from requiring a reference to the service in your long-running task.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜