public class TextSwitcherTest extends View implements ViewFactory { private TextSwitcher mSwitcher; private int mCounter = 0; public TextSwitcherTest(Context context) { super(context); mSwitcher = (TextSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); } public TextSwitcherTest(Context context, AttributeSet attrs) // 수정 AttributeSet attrs 추가 { super(context , attrs); mSwitcher = (TextSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); } public View makeView() { TextView t = new TextView(this); t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); t.setTextSize(36); return t; } }
위와 같이 TextSwitcher를 테스트 해볼려구 하는데
TextView t = new TextView(this); 에서 자꾸 아래와 같은 메시지가 뜨는데
The constructor TextView(ImageViewRotate) is undefined
뭐가 잘못 되었는지 모르겠어요 도움부탁드립니다.
감사합니다. 왜 안되는지 잘 알았습니다.
그런데 실제로 적용해보니 에러는 안뜨는데
자꾸 "응용프로그램이 예상치 않게 중지되었습니다" 라는 메시지가 나오네요;;
수정은 아래와 같이 했습니다.
public class TextSwitcherTest extends View implements ViewFactory { private TextSwitcher mSwitcher; private int mCounter = 0; private Context mContext; //추가 public TextSwitcherTest(Context context, AttributeSet attrs) { super(context); mContext = context; //context 받아옴 mSwitcher = (TextSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); Animation in = AnimationUtils.loadAnimation(context, android.R.anim.fade_in); Animation out = AnimationUtils.loadAnimation(context, android.R.anim.fade_out); mSwitcher.setInAnimation(in); mSwitcher.setOutAnimation(out); }
public View makeView() { TextView t = new TextView(mContext); //수정 t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); t.setTextSize(36); return t; }
}
실질적으로 문제가 되는곳은 setFactory 부분이 아니라 바로 위에
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
이부분으로 생각됩니다.
findViewById는 풀어서 쓰면 android.view.View.findViewById() 입니다.
해당 View 에 들어있는 View중에 자신이 정한 ID값과
일치하는 View를 찾는것이죠....
위에서 findViewById 부분은 새로 생성된 TextSwitcherTest 라는 View에서
R.id.switcher라는 ID를 찾는것이기에 NullPointError가 난것 같습니다.
Layout을 짤때는 View들끼리의 관계들을 생각하면서 만들어야합니다.
어디에 어떤방식으로 불려지고 쓰여지는지는 제가 알 수 없으므로
하나하나 고쳐드릴수는 없을것 같습니다...;;
드로이님 답변 참고로 조금씩 길을 찾아가는 것 같습니다.
이렇게 토론을 하면서 많이 배우는 것 같습니다.
우선 위에 답변드리면
main.xml에 아래와 같이 선언해서 ID 문제는 아닌거 같습니다.
<TextSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="wrap_content" />
에러를 계속 살펴보니 생성자 선언 문제 인 것 같습니다.
main xml 파일에서 TextSwitcherTest View class를 불러 사용하기 위해서 public TextSwitcherTest(Context context, AttributeSet attrs) 생성자를 하나 더 만들었습니다. 여기에
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
을 추가하니까 에러가 뜨네요. 그래서 아래와 같이 수정을 했는데 에러가 없는 대신 아예 동작을 안하네요. 불리는 생성자가 달라서 그런 것 같네요.
정상적인 동작을 하기 위해서는 public TextSwitcherTest(Context context, AttributeSet attrs)에 선언되는 것이 맞는 것 같은데 무엇이 문제인지 모르겠네요. 고수님들 무엇이 문제인지 한번 더 부탁드립니다.
public TextSwitcherTest(Context context) { super(context); mSwitcher = (TextSwitcher) findViewById(R.id.switcher); mSwitcher.setFactory(this); mSwitcher.setText("123"); } public TextSwitcherTest(Context context, AttributeSet attrs) // 수정 AttributeSet attrs 추가 { super(context , attrs); // mSwitcher = (TextSwitcher) findViewById(R.id.switcher); // 삭제 // mSwitcher.setFactory(this); // 삭제 }
TextView의 Constructor 메소드는 TextView(Context con) 입니다.
괄호안에 Context값이 들어가야 하죠...
Activity를 상속받은 클래스에서만 Context부분을 this로 쓰는게 가능합니다...
this대신에 public
TextSwitcherTest(Context context) 이부분에서
받아온 context값을 저장시켜놓고 필요할때 그 값을 넣으시면 됩니다.