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

버튼으로 다른클래스 실행시키는 방법 Intent사용?

0 추천

TestGameActivity 클래스에서 버튼을 만들고  버튼을 눌렀을때

TestView 클래스를 실행시키려고합니다.

지금 소스는 코드에러는 없지만 실행시키고 버튼을 누르면 오류로 종료가 되거든요?

어떻게 해야 버튼을눌렀을때 TestView클래스를 불러올수있을까요?

 

 

 

=======================================================

=======================================================

 

 package com.example.testgame;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class TestGameActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new TestView(this));
        //setContentView(R.layout.main);
       
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT,
          LinearLayout.LayoutParams.MATCH_PARENT);
       
        LinearLayout baseLayout = new LinearLayout(this);
        baseLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(baseLayout,params);
       
        Button btn = new Button(this);
        btn.setText("버튼입니다.");
        btn.setBackgroundColor(Color.MAGENTA);
        baseLayout.addView(btn);
       
        btn.setOnClickListener(new View.OnClickListener() {
   
   public void onClick(View v) {
    // TODO Auto-generated method stub
        
    Intent intent = new Intent(TestGameActivity.this, TestView.class);
    startActivity(intent);

   }
  });  
    }

   
}

 

======================================================

======================================================

 

 

package com.example.testgame;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

 

public class TestView extends View {
 public TestView(Context context) {
  super(context);
 }

 @Override
 public void onDraw(Canvas canvas) {
  Bitmap _android = BitmapFactory.decodeResource(getResources(),
    R.drawable.android);
  canvas.drawBitmap(_android, 0, 0, null);

 }
}

튀맥 (160 포인트) 님이 2013년 11월 21일 질문

1개의 답변

0 추천
baseLayout에 TestView를 addView 해주시면 됩니다.
원조안드로이드 (58,190 포인트) 님이 2013년 11월 22일 답변
...