마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

activity에서 fragment 함수 실행 nullpoint 오류 문의

0 추천

아래의 형태로

Main에서 Fragment가 BluetoothFragment일때

BluetoothFragment의 textview를 변경할려고하는데

아래와 같이 실행을하면 Nullpointexception이 발생합니다.

Fragment에서 textview가 생성되기전에 실행이 되어서 그런건가요?

 수정할수 있는 방안이 없는가요??

fragment가 여러개가 있어서 해당 fragment일경우에만 실행을 하고 싶어서 

아래와 같이 구현을 하였습니다.

 

Mainactivity

BluetoothFragment checkbluetooth = (BluetoothFragment) getSupportFragmentManager().findFragmentById(R.id.bluetooth_fragment);

if(String.valueOf(fragment).contains("BluetoothFragment")) {
    checkbluetooth.BT_Connect();
}

Fragment

ublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
text_bt = (TextView) view.findViewById(R.id.bluetooth_state);
}

public void BT_Connect() {
~~~~~~~~~~
}
public void BT_Disconnect() {
~~~~~~~~~
}

 

쿠쿠부다스 (6,470 포인트) 님이 2017년 8월 18일 질문

1개의 답변

0 추천
if (view == null) view = inflater.inflate(R.layout.foo_layout, container, false)를 text_bt를 할당하는 라인 위에 추가해주시면 됩니다.
minor (13,710 포인트) 님이 2017년 8월 18일 답변
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_bluetooth,container,false);
         text_bt = (TextView) view.findViewById(R.id.bluetooth_state);
       return view;
}
이렇게 말씀이신거같은데...
맞나요?? 이렇게는 진행을 했었습니다.
...