bty99
bty99

Reputation: 11

Android development- Make button open new screen

What I'm trying to do is probably really simple but I've followed several different instructions and cant seem to get it to work. Basically all I want to do is create a button in an xml layout under(main_activity) reference it in java (MainActivity), then set up that button to open its own new xml file. As of now when I click on the button (on my phone not the emulator) is crashes. This is what I have so far. Thanks for looking at it.

xml screen one

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/tvFirst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Application"
    android:textSize="30dp" />

<Button
    android:id="@+id/b1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvFirst"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp"
    android:text="1"
    android:textSize="30dp" />

</RelativeLayout>

java screen1

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b = (Button) findViewById(R.id.b1);

    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, ButtonOne.class);
            startActivity(i);
        }

    });
}
}

xml screen2

<?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" >

<TextView
    android:id="@+id/tvButtonOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is te button one screen" />

</LinearLayout>

java screen2

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    findViewById(R.layout.buttonone);
}

}

Upvotes: 0

Views: 4050

Answers (3)

finiteloop
finiteloop

Reputation: 4496

@javac and @A-C are both right, you need to call setContentView, however also make sure that you have declared the new activity in your AndroidManifest.xml file.

<activity
android:name="NAME"
android:label="@string/string_name"/>

Check out this link to see what the Android docs have to say.

To help you gain a broader understanding, the workflow of what you're trying to do will be like this:

  1. Start activity 1
  2. (in onCreate()) onClickListener gets set for button
  3. button gets pressed
  4. onClickListener runs, calling startActivity()
  5. In startActivity(), the system ensures that you've declared the activity in AndroidManifest.xml, and if you have, it starts the activity.
  6. (in onCreate()) call setContentView(), which tells the new activity how to lay itself out, and also makes calls to findViewById() work as you expect them to.
  7. Call findViewById() to get a reference to the view you want to be able to manipulate.

Upvotes: 1

turnt
turnt

Reputation: 3255

So I guess you have two screens (2 activities) and when the button is clicked it launches screen2. Well on the screen2 class, you do not have a view defined.

Use this line to reference the xml file

setContentView(xmlfilenamehere);

And here is some info on starting a second activity.

http://developer.android.com/training/basics/firstapp/starting-activity.html

Upvotes: 0

A--C
A--C

Reputation: 36449

Your app is crashing because you are calling findViewById(R.layout.buttonone); without giving anything for your new Activity to work with - it can't find R.layout.buttonone because

  • You have not set this Activity's layout
  • You are trying to find a layout file, which is not a view, but an xml that defines how Views are arranged; a layout of View Objects.

What this means is that you need to replace findViewById(R.layout.buttonone); with setContentView (R.layout.buttonone);

From there you can use findViewById() to get the components defined in the xml file, such as tvButtonOne. Make sure that each component you want to access has its own id, otherwise you crash your app again.

Upvotes: 0

Related Questions