프래그먼트 안에서 모듈화...
조회수 931회
프래그먼트 모듈을 해주고 실행을 하면 에뮬에서 앱을 실행하면 꺼집니다 ㅠㅠㅠㅠ 무슨 오류가 있는건가요....
public class ChannelBuskerSchedule extends Fragment {
int test__schedule=5;
public ChannelBuskerSchedule(){
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.activity_channel_busker_schedule, container, false);
for (int scheduleCount=0; scheduleCount<test__schedule; scheduleCount++) {
addSchedulesch(inflater);
Log.e("for문 횟수", String.valueOf(scheduleCount));
}
return v;
}
public void addSchedulesch(final LayoutInflater inflater){
final ImageButton dropdownBtn = (ImageButton)getView().findViewById(R.id.dropdown_sch);
final LinearLayout scheduleBox = (LinearLayout)getView().findViewById(R.id.addSchedule_sch);
if (test__schedule > 1 ){
dropdownBtn.setVisibility(View.VISIBLE);
View list = inflater.inflate(R.layout.schedule_list,scheduleBox,false);
if(list.getParent()!= null)
((ViewGroup)list.getParent()).removeView(list);
scheduleBox.addView(list);
scheduleBox.setVisibility(View.GONE);
dropdownBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
scheduleBox.setVisibility(View.VISIBLE);
dropdownBtn.setVisibility(View.INVISIBLE);
}
});
}
}
}
-
(•́ ✖ •̀)
알 수 없는 사용자
2 답변
-
fragment 의 content view 는 onCreateView 에서 리턴 된 view 와 binding 됩니다. 작성하신 코드 로직을 보면 onCreateView 에서 inflate 된 view 를 return 하기 전에 addSchedulesch() 호출하는데, 이 함수 내에서 getView() 시 view 가 아직 null 이기 때문에 NullPointerException 이 발생할 것으로 보이네요. onCreateView() 가 끝나고 content view 가 설정 된 뒤에 하시거나, infalte 되어 return 된 v 변수를 가지고 하는 방법도 있겠네요.
onActivityCreated() 를 override 하시고 그 함수 내에서 이후 루틴을 처리 하시는 것을 추천 드립니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
-
-
onCreateView에서 inflate한 view를 리턴하기 전에 getView를 하셔서 에러가 발생하는 겁니다. 위에 분 말씀처럼 onActivityCreated()에서 해당 메소드를 호출하세요.
댓글 입력