나는 최근에 안드로이드에서 시작했습니다.버튼 질문
내가 코드
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
finish();
}
});
setContentView(R.layout.main);
}
}
의이 작품을 쓴 내 main.xml에이
<?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:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="ArrangeMe"/>
<Button android:text="Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonOne"></Button>
</LinearLayout>
다음과 같이 보이지만, 내가이 라인을 통과 할 때
this.button1 = (Button)findViewById(R.id.buttonOne);
내가 Button1을 관찰 = null. 하지만 R.id를 입력하면 이클립스는 자동 완성 buttonOne을 제안하지 않습니다 (레이아웃 XML이 맞다고 제안합니다!)
어디서 잘못 될까요?
편집 :
는 interstingly, 난 여전히 버튼이 표시되지 않습니다 다음 코드,
을 시도했다. 충돌이 멈췄지만 버튼이 나타나지 않습니다!
button1 = new Button(getContext());
button1.setText("1");
addView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
finish();
}
});
이 오류를
을주고 있었다 난 당신이 된 setContentView (R.layout.main)를 호출해야한다고 생각
button1 = new Button(getBaseContext());
button1.setText("1");
addContentView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
finish();
}
});