gpt4 book ai didi

java - 绑定(bind)服务出错

转载 作者:行者123 更新时间:2023-11-30 00:34:45 24 4
gpt4 key购买 nike

我只是在玩绑定(bind)服务的示例教程。运行时出现空指针异常错误。

FATAL EXCEPTION: main
Process: com.boundservice.boundservice, PID: 12342
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.boundservice.boundservice.MyService.getCurrentTime()' on a null object reference
at com.boundservice.boundservice.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

下面是我的代码:

Activity :

package com.boundservice.boundservice;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

MyService myService;
boolean isBound = false;
TextView myTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView)findViewById(R.id.myTextView);

Intent intent = new Intent(this, MyService.class); //Start Service
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);


final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String currentTime = myService.getCurrentTime();
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setText(currentTime);
}
});

}


private ServiceConnection myConnection = new ServiceConnection() { //To see whether service is connected or not, if yes it will return true
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
MyService.MyLocalBinder binder = (MyService.MyLocalBinder) service;
myService = binder.getService();
isBound = true;
Log.d("Service", "Service is successfully bound");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
isBound = false;
}
};




}

服务:

package com.boundservice.boundservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MyService extends Service {

public IBinder myBinder = new MyLocalBinder(); //Declare an object of local class

public MyService() {
}

@Override
public IBinder onBind(Intent intent) { //Auto generated method
return null;
}

public String getCurrentTime() { //Simple method to fetch date
SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss MM/dd/yyyy", Locale.US);
Date date = new Date();
String datestring = dateformat.format(date);
Log.d("Service", "Date is : " + datestring);
return (dateformat.format(new Date()));
}

public class MyLocalBinder extends Binder { //Create a local class that will reference this service which in return will be used to access from Activity

MyService getService() {
return MyService.this;
}

}
}

不知道为什么会这样。我是否需要使用处理程序来更新我的 TextView 或不需要。我觉得它没有启动服务并绑定(bind)它。

最佳答案

我认为您错误地覆盖了 onBind() 方法:

@Override
public IBinder onBind(Intent intent) {
return myBinder;
}

关于java - 绑定(bind)服务出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com