2017-12-20 4 views
1

Android Studio에서 만든 서비스에서이 문제가 발생했습니다. 내 firebase 서비스에서 'findViewById (int)'메서드를 확인할 수 없습니다.

enter image description here

내가 새로운 서비스를 만들어, 그것은 된 .java 파일이며, 나는이 서비스 불러 내부 findViewById를 사용할 수 없습니다 내가 내부가 어떻게 든 MainActivity를 참조해야합니다 생각, 난 몰라 그것을하는 방법과 내가 도움이 필요해. 미안하지만 몇 가지 실수가 있지만 나는 안드로이드 개발에 초보자입니다. 감사합니다.

내가

package com.example.qkiri.buttonnum; 

import android.app.Activity; 
import android.util.Log; 
import android.view.View; 
import android.widget.TextView; 

import com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

import static android.content.ContentValues.TAG; 


public class MessageRec extends FirebaseMessagingService { 
    public MessageRec(MainActivity mainActivity) { 
    } 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     int number; 
     Log.d(TAG, "From: " + remoteMessage.getFrom()); 

     number = 23; 
     final TextView txtView2 = (TextView) findViewById(R.id.service); 
     txtView2.setText(number); 

     if (remoteMessage.getData().size() > 0) { 
      Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
     } 

     if (remoteMessage.getNotification() != null) { 
      Log.d(TAG, "Message Notification Body: " + 
      remoteMessage.getNotification().getBody()); 
     } 
    } 
} 
+0

을 .. 활동에 브로드 캐스트 리시버를 구현 업데이트하는 올바른 방법입니다 UI –

+0

서비스 친구 인 경우 잘못된 사용. 크리켓이 제안하는 것을 따르십시오. – hepizoj

답변

0

당신은 서비스에서하지만 활동에서 UI를 업데이트하지 말아야을 만들어 서비스의 코드,하지만 당신이 정말로 필요로하는 경우에 당신은에 시도하기 전에 1 개 단계를 할 필요가 호출 메소드 findViewById를()

1) 인플레이터 참조 얻을 : 지금)

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 

2 findV 전화 기준을 사용 보기 방식 :

View layout = inflater.inflate(R.layout.myLayout, null); 

final TextView txtView2 = (TextView) layout.findViewById(R.id.service); 
+0

그랬지 만 "layout.findViewById (R.id.service)"에서 오류가 발생했습니다. 오류는 "findViewById 메소드는 UI 스레드에서 호출해야하며 현재 유추 된 스레드는 작업자입니다" –

1

서비스에서 사용자가 변경해야하는 경우 프로젝트에서 통신 부품을 개발해야합니다. 통신 부분을 개발하여 활동에서 서비스의 UI 변경 요청을 알 수 있습니다.

구현 방법에는 여러 가지가 있습니다. 브로드 캐스트, 바인더 및 메시지 중 하나를 사용할 수 있습니다.

다음 URL로 자세한 내용을에서 확인하십시오 : 당신이 활동 인스턴스, 여전히 레이아웃을 찾을 수 없을 수도를 사용하더라도 http://developer.samsung.com/galaxy/others/effective-communication-between-service-and-activity