2017-04-25 4 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; 
     } 

    } 
} 

이 일어나는 이유 확실하지. Handler를 사용하여 TextView를 업데이트해야하거나 필요하지는 않습니다. 나는 그것이 서비스를 시작하고 그것을 묶어 두지 않는다고 느낍니다.

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

[NullPointerException은 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and- 어떻게 - 할 - 고정 - 그것) – X3Btel

답변

2

내가 잘못 onBind() 방법을 재정의 생각합니다. "onBind"에서 "null"을 반환했습니다. "바인더 객체"를 반환해야합니다.

관련 문제