J'ai récemment commencé sur android.bouton question
j'ai écrit ce morceau de code
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);
}
}
et mon main.xml regarde comme ci-dessous
<?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>
mais quand je passe par cette ligne
this.button1 = (Button)findViewById(R.id.buttonOne);
i observer button1 = null. mais quand je tape R.id. éclipse suggère un bouton complet automatique Un (qui suggère que la mise en page xml est correcte!)
où est-ce que je vais mal?
modifier:
interstingly, j'ai essayé le code suivant,
encore le bouton ne semble pas. il a arrêté de s'écraser, mais le bouton n'apparaît pas!
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();
}
});
cette erreur a été de donner
je les ai changé pour
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();
}
});