开发者

Null Pointer When Access Data Structure Located Within Android Service

I have a service running in my Android application which contains a HashMap that I would like to use in an Activity. So I bound the service to my a开发者_如何学运维ctivity and created a link between to two in order to use the data structure.

This is how I did it so far:

Activity (relevant code):

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        imService = ((IMService.IMBinder) service).getService();            
    }

    public void onServiceDisconnected(ComponentName className) {
        imService = null;
        Toast.makeText(ViewFlipperTest.this,
                R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }
};


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    bindService(new Intent(ViewFlipperTest.this, IMService.class),
            mConnection, Context.BIND_AUTO_CREATE); 
                 .
                 .
                 .
}
protected void onResume() {
    super.onResume();
    bindService(new Intent(ViewFlipperTest.this, IMService.class),
            mConnection, Context.BIND_AUTO_CREATE);     
}

private void getMap(ViewFlipper flipper2) {

    Map<Integer, internalChatObj> tempMap = imService.getMapwData();
   }

Service (relevant code):

public class IMService extends Service implements IAppManager{
                .
                .
                .

public Map getMapwData(){
    if(getNumberOpenChats()>0){
        return openChatMapwData;
    }
    else
        return null;

}
}

Each time I try and run the method getMap(), I get a null pointer. I even put a check to see if the imService was null and it was null each time. It seems that it is trying to obtain the data from the Service but the binding to the service is null, is that right? I have a check in the Service to send null if the map is empty, so that shouldn't be a problem.

Any help?

Edit based on Will's tips New code

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
        imService = ((IMService.IMBinder) service).getService();
        dialog.dismiss();
    }

    public void onServiceDisconnected(ComponentName className) {
        imService = null;
        Toast.makeText(ViewFlipperTest.this,
                R.string.local_service_stopped, Toast.LENGTH_SHORT).show();
    }
};

public void onCreate(Bundle savedInstanceState) {
    dialog = ProgressDialog.show(getApplicationContext(), null,"Loading..", true, false);
}

Edit II

IMBinder Class (Seems standard, no?)

public class IMBinder extends Binder {
    public IAppManager getService() {
        return IMService.this;
    }
}

Is it necessary to have IMService.this instead of this?


It really seems like a timing issue; you shouldn't call the getMap function unless you are certain the imService variable exists, which is after a call to onServiceConnected.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜