안드로이드 개발 질문/답변
(글 수 45,052)
직접 Button 클래스를 상속해서 버튼을 화면에 뿌려주는 것까지는 했습니다.
그런데 그 Button 클래스내에서 click event를 설정했을 때 click 이벤트는 발생하나 R 클래스를 사용하여 TextView를 가져오게 되면 그 TextView 클래스가 null 이네요
public void onClick(View v) {
String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
즉 위의 소스에서 tv 가 null 이네요. 혹시 해결 방법을 아시는 분은 알려주세요
완전 한 소스는 아래와 같습니다.
public class AndirodTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonTest bu1 = (ButtonTest)findViewById(R.id.Button00);
bu1.setClickEvent();
}
}
public class ButtonTest extends Button {
public ButtonTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ButtonTest(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ButtonTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public void setClickEvent(){
this.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/testTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.test.ButtonTest
android:id="@+id/Button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonTest0">
</com.test.ButtonTest>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello">
</Button>
</LinearLayout>
그런데 그 Button 클래스내에서 click event를 설정했을 때 click 이벤트는 발생하나 R 클래스를 사용하여 TextView를 가져오게 되면 그 TextView 클래스가 null 이네요
public void onClick(View v) {
String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
즉 위의 소스에서 tv 가 null 이네요. 혹시 해결 방법을 아시는 분은 알려주세요
완전 한 소스는 아래와 같습니다.
public class AndirodTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ButtonTest bu1 = (ButtonTest)findViewById(R.id.Button00);
bu1.setClickEvent();
}
}
public class ButtonTest extends Button {
public ButtonTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ButtonTest(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ButtonTest(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public void setClickEvent(){
this.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/testTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.test.ButtonTest
android:id="@+id/Button00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonTest0">
</com.test.ButtonTest>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello">
</Button>
</LinearLayout>
2009.12.23 15:37:03
버튼 상속해서 하는방법도 있군요 ㅡㅡㅋ
public class AndirodTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bu1 = (Button)findViewById(R.id.Button00);
bu1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
});
}
}
---------------------- 이렇게 하시면 될것 같습니다. 빨간부분은 import시켜주시고 그리고
그냥 친거기때문에 혹시 오타날수도 있지만 그건 수정해주시면 될것 같습니다.
public class AndirodTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bu1 = (Button)findViewById(R.id.Button00);
bu1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
//String abc=getResources().getString(R.string.app_name);
TextView tv = (TextView)findViewById(R.id.testTextView1);
tv.setText("11");
}
});
}
}
---------------------- 이렇게 하시면 될것 같습니다. 빨간부분은 import시켜주시고 그리고
그냥 친거기때문에 혹시 오타날수도 있지만 그건 수정해주시면 될것 같습니다.