简体   繁体   中英

New activity in Android Studio wont start

While creating my app I have run into the problem that after changing to a second activity, I can't seem to change to a third. I have tried modifying the function all the way down to a simple call of the activity; however I still run into the problem that I can click on the button in my emulator but nothing happens. Thoughts?

Java:

public class NewGameActivity extends AppCompatActivity {

    public Spinner players, bestOf;
    public Button startButton;

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

        players = (Spinner) findViewById(R.id.players);
            Integer[] items1 = new Integer[]{2,3,4,5,6,7,8,9,10};
            ArrayAdapter<Integer> adapter1 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items1);
            players.setAdapter(adapter1);

        bestOf = (Spinner) findViewById(R.id.bestOf);
            Integer[] items2 = new Integer[]{1,3,5,7,9,11,13,15,17,19,21,23,25};
            ArrayAdapter<Integer> adapter2 = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, items2);
            bestOf.setAdapter(adapter2);

        startButton =(Button)findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new_game, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void sendMessageStart(View view) {
        int playerNum = Integer.valueOf(players.getSelectedItem().toString());
        int games = Integer.valueOf(bestOf.getSelectedItem().toString());

        RadioGroup group;
        group = (RadioGroup) findViewById(R.id.group);

        int selectedId = group.getCheckedRadioButtonId();

        Bundle bundle = new Bundle();
        bundle.putInt("players", playerNum);
        bundle.putInt("games", games);

        if (selectedId == 0) {
            Intent intent = new Intent(this, ThreeSignGameActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
        else {
            Intent intent = new Intent(this, FiveSignGameActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }

    }
}

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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="royrowland.zerosum.NewGameActivity"
android:background="#0169B2">

<TextView
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGamePlayers"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="30dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"
    />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/players"
    android:layout_alignTop="@+id/textView4"
    android:layout_toRightOf="@+id/textView4"
    android:layout_toEndOf="@+id/textView4"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGameStyle"
    android:id="@+id/textView6"
    android:layout_marginTop="60dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"
    android:layout_below="@+id/textView4"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/textView6"
    android:layout_centerHorizontal="true"
    android:checkedButton="@+id/radio3"
    android:id="@+id/group">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio3"
        android:id="@+id/radio3"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="20sp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio5"
        android:id="@+id/radio5"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="20sp" />
</RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/newGameBestOf"
    android:id="@+id/textView7"
    android:layout_below="@+id/textView6"
    android:layout_alignLeft="@+id/textView6"
    android:layout_alignStart="@+id/textView6"
    android:layout_marginTop="80dp"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="40sp"/>

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/bestOf"
    android:layout_alignTop="@+id/textView7"
    android:layout_toRightOf="@+id/textView7"
    android:layout_toEndOf="@+id/textView7"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:text="@string/newGameButtonText"
    android:id="@+id/startButton"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:typeface="sans"
    android:textSize="30sp"
    android:onClick="sendMessageStart"/>

XML contains android:onClick="sendMessageStart" and you have implement your logic in its method sendMessageStart and You are also getting the Button object and setting a listener to that Button .
startButton =(Button)findViewById(R.id.startButton);

Try delete this code

    startButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM