Adam
Adam

Reputation: 1

Buttons disappear Android

Everything works fine on the first page but when I press the button the 2nd page loads but the button wont appear... No crashes or errors.

Public void onClick(View v)
{
    switch (v.getId())  
    {  
    case R.id.button2:  
        finish();  
        break;    
    case R.id.button1:  
        Intent game1 =new Intent(this, game.class);  
        this.startActivity(game1);  
                            }

Then I display page 2

public class game extends Activity implements OnClickListener{

public void OnCreate(Bundle icicle)
{
    super.onCreate(icicle);
    setContentView(R.layout.game1);
    Button b1= (Button)findViewById(R.id.b1);// Doesn't show up
    b1.setOnClickListener(this);

}
public void onClick (View v)
    {
        switch (v.getId())
        {
        case R.id.b1:
            setResult(RESULT_OK);
            finish();
        break;


        }

    }
}

The button is a no-show... and I don't understand why. Any response is helpful.

Edit: Heres the XML for game1

< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:background="@color/white" >

<Button
    android:id="@+id/b1"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:onClick="myClick"
    android:text="Exit" />

</LinearLayout>

Edit: And the layout for main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<Button
    android:id="@+id/button1"
    android:layout_width="104dp"
    android:layout_height="wrap_content"
    android:text="Start" />


<Button
    android:id="@+id/button2"
    android:layout_width="99dp"
    android:layout_height="wrap_content"
    android:text="Exit" 
    android:onClick="myClick"
    />

</LinearLayout>

Upvotes: 0

Views: 1653

Answers (2)

KKC
KKC

Reputation: 548

Problem is with your linear layout Try this it will work..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Upvotes: 0

KKC
KKC

Reputation: 548

Intent game1 =new Intent(this, game.class);

instead of this line try like this i think it will work

Intent game1 =new Intent(yourClassName.this, game.class);

Upvotes: 1

Related Questions