setOnClickListener中的startActivity()没有做任何事情

时间:2016-09-04 21:47:10

标签: android android-intent android-activity

我在视图中有一个按钮来检查一些约束,然后启动另一个活动,但是startActivity()调用没有做任何事情,新活动的onCreate()永远不会被调用,然后是代码继续过去。我已经检查了使用日志记录和断点,并且正在满足条件并且正在调用startActivity()。这两个活动都在应用程序清单中定义。

来自源活动的onCreate():

int[] winning_player;
int next_player;
int[] scores;

[...]

final Button end_turn_button = (Button) findViewById(R.id.end_turn);
end_turn_button.setOnClickListener(
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (winning_player[0] == next_player) {
                Intent intent = new Intent(getApplicationContext(), FinalScreenActivity.class);
                intent.putExtra("SCORES", scores);
                startActivity(intent);
            }
       }
});

FinalScreenActivity.java

public class FinalScreenActivity extends AppCompatActivity {

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

    Intent intent = getIntent();
    final int[] scores = intent.getIntArrayExtra("SCORES");

    // put the scoreboard in ascending order
    Arrays.sort(scores);

    // find the scoreboard in the view
    TableLayout scoreboard = (TableLayout) findViewById(R.id.scoreboard);

    // get the string resources to be formatted
    Resources res = getResources();
    String player_name_format = res.getString(R.string.player);

    // Set the scoreboard in the view
    Log.d("brains.PlayActivity", "onCreate: Creating scoreboard");
    // for each player entry in the scoreboard
    for (int i = 0; i < scores.length; i++) {
        Log.d("brains.PlayActivity", "onCreate: adding scoreboard entry "+String.valueOf(i));

        // create 2 TextViews
        TextView player_name = new TextView(FinalScreenActivity.this);
        TextView player_score = new TextView(FinalScreenActivity.this);

        // Set the size of the TextViews
        player_name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

        // set the first one to the player's name string resource
        player_name.setText(String.format(player_name_format, i + 1));
        // set the second one to the player's score resource, which takes the score twice (once for the number itself, and once to work out which plural is needed)
        player_score.setText(res.getQuantityString(R.plurals.brains, scores[i], scores[i]));

        // Create a TableRow to put the score into
        TableRow scoreboard_entry = new TableRow(FinalScreenActivity.this);
        // Set the size of the TableRow
        scoreboard_entry.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

        // Add the TextViews to the TableRow
        scoreboard_entry.addView(player_name);
        scoreboard_entry.addView(player_score);

        // Add the TableRow to the TableLayout
        scoreboard.addView(scoreboard_entry);
    }

    // Get the title text
    TextView title_text = (TextView) findViewById(R.id.title_text);
    title_text.setText(res.getQuantityString(R.plurals.win_brains, scores[scores.length - 1], scores.length - 1, scores[scores.length - 1]));

    // set the play again button
    Button play_again_button = (Button) findViewById(R.id.play_again_button);
    play_again_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getBaseContext(), SelectPlayersActivity.class);
            startActivity(intent);
        }
    });

}

}

和activity_final_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="uk.co.bluesapphiremedia.android.zombiedice.FinalScreenActivity"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/title_text"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/scoreboard"
        android:layout_below="@+id/title_text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/play_again"
        android:id="@+id/play_again_button"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

end_turn_button.setOnClickListener(this);

//override onClick() method in activity implement interface View.OnClickListner
public void onClick(View v) {
        if(v.getId() == R.id.end_turn){
          if (winning_player[0] == next_player) {
                Intent intent = new Intent(this,FinalScreenActivity.class);
                intent.putExtra("SCORES", scores);
                startActivity(intent);
            }

       }
}