我正在玩一个绑定服务的示例教程。在运行时我得到Null Pointer Exception错误。
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)
以下是我的代码:
活动:
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或不需要。我觉得它没有启动服务并绑定它。
答案 0 :(得分:1)
我认为您错误地覆盖了onBind()
方法:
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}