远程服务不绑定/asInterface 返回“null”

发布于 2024-12-05 18:04:40 字数 2722 浏览 1 评论 0原文

我在本书中的示例之后创建了一个远程服务并尝试绑定它在此活动中:

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);
}
}

但是 mBrowsserRemoteService 为 null(在 onClickSendLogin 方法中)并返回 NullPointerException,我不知道为什么?服务中的 onBind 方法似乎也从未被调用过。我的问题出在哪里。

提前致谢 泰德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2024-12-12 18:04:40

您收到 NullPointerException 的原因是因为您正在执行

mBrowserRemoteService.call_course_overview();

而不考虑 mbound; mbound 此时可能为真或为假。

我怀疑更大的问题是您无法绑定到此服务,很可能是因为您的 AndroidManifest.xml 配置不正确。如果您仍然遇到问题,请发布您的 AndroidManifest 文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文