0

내 응용 프로그램에서 boundService를 만들려고하지만 null이 반환되는 것과 관계없이 결과를 다시 얻을 수 없습니다. 내 서비스에서 코드가 제대로 실행된다는 것을 디버깅에서 볼 수 있지만 내 활동이 너무 빠름을 의심해 서비스를 호출 할 때 아직 제공 할 것이 없습니다. 이 문제를 어떻게 해결할 수 있습니까?BindService는 순수 문자열을 가리킬 때도 nullpointer를 throw합니다.

이것은 내 활동입니다.

import com.example....DownloadPicService.MyBinder; 

import java.io.IOException; 


public class RecievedSB extends AppCompatActivity { 
    public DownloadPicService Dlpic; 
    private LoginActivity.UserLoginTask mAuthTask = null; 
    private FirebaseAuth mAuth; 
    private FirebaseAuth.AuthStateListener mAuthListener; 
    private static final String TAG = "Login"; 
    private DatabaseReference mPostReference; 
    private TextView recievedCardTextView; 
    private String path1; 

    /** Messenger for communicating with the service. */ 
    DownloadPicService mService; 

    /** Flag indicating whether we have called bind on the service. */ 
    boolean mBound; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_recieved_sb); 
     mPostReference = FirebaseDatabase.getInstance().getReference() 
       .child("users").child(".."); 
     mAuthListener = new FirebaseAuth.AuthStateListener() { 
      @Override 
      public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
       FirebaseUser user = firebaseAuth.getCurrentUser(); 
       if (user != null) { 
        // User is signed in 
        Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 
       } else { 
        // User is signed out 
        Log.d(TAG, "onAuthStateChanged:signed_out"); 
       } 
       // ... 
      } 
     }; 
     recievedCardTextView = (TextView) findViewById(R.id.recievedCardTextView); 


    } 
    @Override 
    public void onStart(){ 
     super.onStart(); 

     if (mAuthTask != null) { 
      return; 
     } 
     ValueEventListener postListener = new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       // Get Post object and use the values to update the UI 
       CardService cid = dataSnapshot.getValue(CardService.class); 
       recievedCardTextView.setText(cid.email); 
       System.out.println(cid.email); 
       // if (mBound) { 

       // } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 
       // Getting Post failed, log a message 
       Log.w(TAG, "loadPost:onCancelled", databaseError.toException()); 
       // ... 
      } 
     }; 
     mPostReference.addValueEventListener(postListener); 

     Intent intent = new Intent(this, DownloadPicService.class); 
     // intent.putExtra("FBservice", "DL"); 
     this.startService(intent); 
     this.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
    } 
    @Override 
    protected void onStop() { 
     super.onStop(); 
     unbindService(mConnection); 
    } 
    private ServiceConnection mConnection = new ServiceConnection() { 
     @Override 
     public void onServiceConnected(ComponentName className, IBinder service) { 
      // This is called when the connection with the service has been 
      // established, giving us the object we can use to 
      // interact with the service. We are communicating with the 
      // service using a Messenger, so here we get a client-side 
      // representation of that from the raw IBinder object. 
      mBound = true; 
      MyBinder myBinder = (MyBinder) service; 
      mService = myBinder.getService(); 


      try { 
       path1 = Dlpic.getImagePath2(); 
       Log.d(TAG, path1); 
      } 
      catch(IOException e) 
      { 
       System.out.println(e.getMessage()); 
      } 

     } 

     @Override 
     public void onServiceDisconnected(ComponentName className) { 
      // This is called when the connection with the service has been 
      // unexpectedly disconnected -- that is, its process crashed. 
      mService = null; 
      mBound = false; 
     } 
    }; 

} 

내 서비스 : 당신은 Dlpic이 코드를 업데이트 초기화되지 않은

public class DownloadPicService extends Service { 
    FirebaseStorage storage = FirebaseStorage.getInstance(); 
    StorageReference storageRef = storage.getReferenceFromUrl("gs:/..."); 

    private static final String TAG = "DLService"; 
    private FirebaseAuth.AuthStateListener mAuthListener; 
    private String path1; 
    private IBinder mBinder = new MyBinder(); 
    private String path2; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 
    @Override 
    public IBinder onBind(Intent intent) { 
     Log.v(TAG, "in onBind"); 
     return mBinder; 
    } 

    public DownloadPicService() { 
     try { 
      getImagePath2(); 
      Log.d(TAG, "Starter DL"); 

     } catch (IOException e) { 
      System.out.println(e.getMessage()); 
     } 
    } 

    public String getImagePath2() throws IOException { 
     StorageReference islandRef = storageRef.child("/userimages/[email protected]/[email protected]"); 

     File localFile = File.createTempFile("images", "jpg"); 
     path1 = localFile.getAbsolutePath(); 

     Log.d(TAG, path1); 
     islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 
       Log.d(TAG, "worked buddy"); 
       //path2 = path1; 
       /*Intent pic = new Intent(DownloadPicService.this, RecievedSB.class); 
       pic.putExtra("picpath", path1); 
       LocalBroadcastManager.getInstance(DownloadPicService.this).sendBroadcast(pic);*/ 
       } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       // Handle any errors 
      } 
     }); 
     return path1; 
    } 
    public String getPath(){ 

     return "hej"; 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 
     Log.d(TAG, "in onUnbind"); 
     return true; 
    } 

    public class MyBinder extends Binder { 
     public DownloadPicService getService() { 
      return DownloadPicService.this; 
     } 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.v(TAG, "in onDestroy"); 
    } 

    @Override 
    public void onRebind(Intent intent) { 
     Log.v(TAG, "in onRebind"); 
     super.onRebind(intent); 
    } 
} 
+0

logcat 게시 – Raghavendra

+1

정말 옳습니다. mService를 호출해야합니다. 이제 모든 것이 작동합니다. 고맙습니다!!! :) – NicklasN

답변

1

,

Dlpic.getImagePath2(); to 

mService.getImagePath2(); 

시도.

관련 문제