Callback button in Android not working
I'm pretty new to Android and I'm trying to program some very easy application with a button and a callback to a new Intent (a new window basically).
This is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Onclick "Start Game" button
start = (Button) findViewById(R.id.startGame);
tv = (TextView) findViewById(R.id.textView1);
start.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
tv.setText("clicked");
Intent i = new Intent(MainActivity.this, Level1Activity.class);
startActivity(i);
}
});
[...]
As you can see I have a button (startGame) and a textView (textView1). My problem is simply that when I click on the startGame button, nothing happens (neither the TextView changes to "clicked" nor the screen changes). The button as well as the TextView are set up correctly since Eclipse doesn't complain,开发者_开发问答 but still the thing doesn't work.
Here is the XML snippet of the view:
<Button android:id="@+id/startGame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Game"></Button>
<TextView android:text="change" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
I believe my problem is really stupid and I just can't see it.
Thanks
EDIT:
I discovered that the problem was related to the fact that I have more than one button in the view, thus more than one callback (on different buttons of course). As soon as I commented the other callbacks, the startGame
button started working as expected.
you have to add in your manifest file one tag then it will work fine. And the tag is ->
- activity android:name="Level1Activity"
remember add this tag within application tag of your manifest file,rest your code is fine. Try it definitely going to work.
try this and tell what will be happened.
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
tv.setText("clicked");
Intent i = new Intent(MainActivity.this, Level1Activity.class);
startActivity(i);
}
});
Are you sure your Manifest is correct? Else this is gonna work for sure...
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(view.getContext(), Level1Activity.class);
startActivityForResult(i, 0);
}
});
精彩评论