开发者

Remote service does not Bind / asInterface returns "null"

开发者 https://www.devze.com 2023-04-07 08:57 出处:网络
I created a Remote Service after the example in this book and try to bind it with in this activity: public class TuCanMobileActivity extends Activity {

I created a Remote Service after the example in this book and try to bind it with in this activity:

public class TuCanMobileActivity extends Activity {
/** Called when the activity is first created. */
//private HTTPSbrowser mBrowserService;
private HTTPBrowserRemote mBrowserRemoteService;
private Boolean mbound=false;
private ServiceConnection mBrowserRemoteServiceConnection = 
    new ServiceConnection() {           
        @Override
        public void onServiceDisconnected(ComponentName name) {
            try {
                mBrowserRemoteService.unregister_course_callback(courseCallback);
            } catch (RemoteException e) {}
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBrowserRemoteService=HTTPBrowserRemote.Stub.asInterface(service);
            mbound=true;
            try {
                mBrowserRemoteService.register_course_callback(courseCallback);

            }
            catch (RemoteException e) {
                // TODO: handle exception
            }
        }

};

private final IClassesCallback courseCallback =
    new IClassesCallback.Stub() {
        @Override
        public void expressClassesdata(HTTPSResponse ClassesResponse)
                throws RemoteException {
            Toast.makeText(TuCanMobileActivity.this, "Just works??", Toast.LENGTH_SHORT).show();

            final TextView txtLoginName = 
                (TextView) findViewById(R.id.textView1);
            txtLoginName.setText(ClassesResponse.HTMLResponse);
        }
开发者_运维百科    };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
public void onClickSendLogin (final View sfNormal) {
    final Intent browserIntent = new Intent(TuCanMobileActivity.this,HTTPBrowserRemoteImpl.class);

    this.bindService(browserIntent, mBrowserRemoteServiceConnection, Context.BIND_AUTO_CREATE);
    if(mbound==true){
        Toast.makeText(TuCanMobileActivity.this, "Service Bound", Toast.LENGTH_SHORT).show();
    }
    else {
        Toast.makeText(TuCanMobileActivity.this, "Service NOT Bound", Toast.LENGTH_SHORT).show();
    }
    try {

        mBrowserRemoteService.call_course_overview();


    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    /*final Intent i = new Intent(this,MainMenu.class);
    startActivity(i);*/
    unbindService(mBrowserRemoteServiceConnection);
}
}

But the mBrowsserRemoteService is null (in the method onClickSendLogin) and returns a NullPointerException and I don't know why? It also seems that the onBind method in the service is never called. Where is my problem.

Thanks in advance Tyde


The reason you are getting a NullPointerException is because you are executing

mBrowserRemoteService.call_course_overview();

regardless of mbound; mbound may be true or false at that point.

I suspect a bigger problem is that you are unable to bind to this service most likely because your AndroidManifest.xml is not configured correctly. If you're still having issues, please post your AndroidManifest file.

0

精彩评论

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

关注公众号