2017-04-25 87 views
2

我只是玩弄綁定的服務的示例教程。在運行時,我得到空指針異常錯誤。錯誤綁定的服務

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或不是必需的。我覺得它並沒有啓動服務和綁定它。

+2

的可能的複製[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-我如何解決它) – X3Btel

回答

2

我認爲你是正確重寫onBind()方法:

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

好吧,我錯了。我在「onBind」中返回「null」。我應該返回「粘合劑對象」