나는 프 래그 멘트가있는 Activity가 있습니다. 이 조각은 예를 들어, 무슨 일이 일어나고 있는지에 따라 레이아웃의 변화 :setOnClickListener가 실행 시간에 nullPointerException을 throw합니다.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = null;
switch(nLesson)
{
case 1:
switch(nPage)
{
case 0: v = inflater.inflate(R.layout.lesson, container, false);
break;
case 1: v = inflater.inflate(R.layout.basic, container, false);
break;
case 2: v = inflater.inflate(R.layout.whatever, container, false);
doIt();
break;
case 3: v = inflater.inflate(R.layout.lesso, container, false); break;
}
break;
}
return v;
}
나는 convenied로 nLesson 및 nPage 값을 변경하고 나는 그것을 상쾌하고
activity.getFragmentManager().beginTransaction()
.detach(instance)
.attach(instance)
.commit();
을 사용하고 있습니다.
그러나 이러한 레이아웃 중 하나에서 내가 작업하고 싶은 몇 가지 의견이 있습니다. 뷰 중 하나에서 onClickListener를 설정하려고하지만 nullPointerException을 표시하고 컴파일 타임이 아닌 앱을 실행할 때 중단됩니다. "onCreateView
", "onStart
", "onResume
", "onActivityCreated
"등에서 onClickListener를 설정하려고했습니다. 어떤 생각?
고맙습니다.
수정 : 사람들이 저에게 물어 보았던 몇 가지 자료를 추가하고 있습니다.
중단되는 레이아웃은 "무엇이든지"라고 불리는 레이아웃입니다. 각 tablerow
(즉, 8x8 = 64 제곱, 체스 판)에 8 tablerows
및 8 imagebuttons
인 tablelayout
입니다. 리스너를 추가하려고하면 모든 사각형이 null로 설정됩니다. 그것은이에 관련이 있기 때문에 그것을 게시하지 않습니다
public void doIt()
{
chessHandler = new ChessHandler(cActivity);
chessHandler.initialize();
}
chessHandler 내 자신의 클래스입니다 :
그들은 모두이 중단 자바 코드
<ImageButton
android:id="@+id/squareA8"
android:contentDescription="@string/r8c1"
android:layout_height="35dp"
android:layout_width="35dp"
android:tag="true"/>
유사하다 케이스. 이러한 것과 같은 64 개 라인, A8은 여전히 findViewById
후 널 (null)로 설정되어 있기 때문에 setOnClickListener
휴식이 있습니다
a8 = (ImageButton)activity.findViewById(R.id.squareA8);
a8.setOnClickListener(new on_click(a8, a,8));
:이 흥미로운 코드의 일부이다. 전형적인 onCreateView()
에서
스택 추적을 게시 할 수 있습니까? – Bobbake4
레이아웃을 게시 할 수 있습니까? – mmlooloo
문제가있는'setOnClickListener()'가있는 곳에 코드를 게시 할 수 있습니까? – laalto