开发者

android service is NullPointerException

开发者 https://www.devze.com 2023-03-18 09:24 出处:网络
who can help me? Why bindService() on onResume() of Activity will be create? How to control the create in date of bindService()?

who can help me?

Why bindService() on onResume() of Activity will be create?

How to control the create in date of bindService()?

Please help to solve below query. I would like to use bindService() method after bindService(),

private TestService ts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("ServiceActivity onCreate()");
        bindService(new Intent(this, TestService.class), c开发者_Go百科onn, BIND_AUTO_CREATE);
        ts.setNum(0);//here is throw nullPointerException
    }
private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
             ts = ((MyBinder)service).getService();
        }
    };

AndroidManifest.xml

<activity android:name=".ServiceActivity" android:label="ServiceActivity">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>
<service android:name="org.caau.android.service.TestService">
</service>


I'm not an expert in this bindService thing but I went to the Android Developer website and read some.

As far as I can see you're missing a in your manifest file for services. --- from website --- Global access to a service can be enforced when it is declared in its manifest's tag. By doing so, other applications will need to declare a corresponding element in their own manifest to be able to start, stop, or bind to the service.

In addition, a service can protect individual IPC calls into it with permissions, by calling the checkCallingPermission(String) method before executing the implementation of that call.

See the Security and Permissions document for more information on permissions and security in general. --- from website ---

Since I'm not very sure about the rest of your code I suggest you should look at the examples Android provides here Local Service Sample

Or you can debug some yourself. Put some logs in the onServiceConnected, and some just before you do ts.setNum

0

精彩评论

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

关注公众号