Code Vader
Code Vader

Reputation: 755

Press Button to go to new Screen

this is my first time developing in Android. I'm trying to create a button that should open a new screen when pushed. I tried following the Android tutorials, as well as read a few of the questions on this site but I'm still struggling. Sorry if the question is redundant to others.

I got this so far:

MainActivity.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {


        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }

        public void scoreGame (View view)
        {
            Intent intent = new Intent(getActivity(), ScoreNewGame.class);

            startActivity(intent);
        }
    }

}

fragment_main.xml

<LinearLayout 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"
    android:orientation="horizontal" >

    <Button 
        android:id="@+id/score_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/score_button"
        android:onClick="scoreGame"/>

</LinearLayout>

ScoreNewGame.java

package com.example.squashbot;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class ScoreNewGame extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        setContentView(R.layout.activity_score_new_game);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_score_new_game,
                    container, false);
            return rootView;
        }
    }

}

fragment_score_new_game.xml

<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"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

I'm pretty new with this so go gentle. Thanx in advance for any help, much appreciated

Upvotes: 2

Views: 320

Answers (2)

Blo
Blo

Reputation: 11978

The app crashes if you push the button? Then, it's because the method scoreGame from android:onClick="scoreGame" attribute (on your Button into fragment_main.xml) must to be attached to your Activity and not Fragment, as follows:

    //...

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }

    }
    // end of PlaceholderFragment

    public void scoreGame (View view) {
        Intent intent = new Intent(this, ScoreNewGame.class);
        startActivity(intent);
    }

}
// end of MainActivity  

NOTE: you should post the stacktrace, it will be better to help you. Hope this helps.

Upvotes: 1

Edwin O.
Edwin O.

Reputation: 5276

Everything looks ok with your code, check if you have registered the fragment_score_new_game host activity in the AndriodManifest file in root folder. look at this code for example

<activity
            android:name=".ScoreNewGame"
            android:label="@string/app_name" >
        </activity>

Upvotes: 0

Related Questions