안드로이드 개발 질문/답변
(글 수 45,052)
public class Test extends Activity { private static final String TAG = "Test"; private TestView testView; private TestMethod testMethod; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); testView = new TestView(this); setContentView(testView); } }
public class TestView extends View{ public TestView(Context context) { super(context); } protected void onDraw(Canvas canvas) { Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.icon); canvas.drawColor(Color.WHITE); canvas.drawBitmap(bm, 0, 0, null); testMethod = new TestMethod(); testMethod.triggerMethod(); } public void methodTest() { Log.i(TAG,"OK"); } }
public class TestMethod { private TestView testView; public void triggerMethod(){ testView.methodTest(); } }
실행하면 TestView에서 TestMethod 클래스의 triggerMethod를 호출하면 TestMethod 클래스에서 다시 TestView 클래스의 methodTest 라는 메소드를 호출 하도록 만들고 싶은데, View 를 상속한 TestView 클래스는 어떤 식으로 객체화 해야 하나요? 지금 현재는 NullPointError 입니다.
2010.11.25 18:00:30
TestMethod 클래스에 setTestView(TestView a){ testView = a;} 생성하고
this.setTestView(testView); 이렇게 메소드를 호출하면
역시나 nullpoint error 같은데요...^^; 물론 호출 하려는 메소드를 TestView의 static 메소드로 작성하면 되지만
전 위와 같이 TestView를 New 해서 객체화해서 사용하고 싶은데 방법이 없을까요? testView = new TestView(); 할려니
TestView 생성자에 선언한 Context때문에 안되던데 어떻게 해야 할까요?
testMethod 에 testView 를 등록하시면 되겠네요..
testMethod.setTextView(this) -> setTExtView(TestView a){ testView=a;}..
이렇게 하면 될 것 같네요.