意图中的错误,app会自动关闭

时间:2015-08-29 11:34:42

标签: android android-intent

最近我进入了应用程序开发的世界。不幸的是,我对第一个意图感到困惑。我尝试将数字选择器(在主活动中)中的信息发送到用户可以定义播放器名称的页面(addPlayersActivity)。在模拟应用程序时,它会在单击主活动上的按钮后关闭。删除intent的接收端代码时,addPlayersActivity会打开而不会出现任何问题。目的是将意图和玩家数量放在一个整数中。为了测试这个,我在接收Activity时创建了一个TextField,它应该显示整数的值。

    public class MainActivity extends Activity {

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

    //Get the widgets reference from XML layout
    final TextView numberofplayers = (TextView) findViewById(R.id.selectNumberPlayers);
    NumberPicker np = (NumberPicker) findViewById(R.id.np);

    //Populate NumberPicker values from minimum and maximum value range
    //Set the minimum value of NumberPicker
    np.setMinValue(2);
    //Specify the maximum value/number of NumberPicker
    np.setMaxValue(8);

    //Gets whether the selector wheel wraps when reaching the min/max value.
    np.setWrapSelectorWheel(true);

    //Set a value change listener for NumberPicker
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal){
            //Display the newly selected number from picker
            numberofplayers.setText("# of Players: " + newVal);
        }
    });

}

public void onClick(View view){
    Intent getPlayerNamesIntent = new Intent(this, AddPlayersActivity.class);

    NumberPicker np = (NumberPicker)findViewById(R.id.np);
    Integer numberOfPlayers = np.getValue();
    getPlayerNamesIntent.putExtra("NUM_PLAYERS", numberOfPlayers);

    startActivity(getPlayerNamesIntent);
}
}

addPlayersActivity的代码

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;

    public class AddPlayersActivity extends AppCompatActivity {


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

    int numberOfPlayers = getIntent().getExtras().getInt("NUM_PLAYERS");

    TextView chosenPlayers = (TextView)findViewById(R.id.chosenPlayers);
    chosenPlayers.setText(numberOfPlayers);

}

public void onClick(View view){
    Intent intent1 = new Intent(this, ChallengesActivity.class);
    startActivity(intent1);
}
}

如果这个问题含糊不清,我想提前道歉,我刚开始,随着时间的推移,我会提高自己。

伊沃

修改 发现错误:整数值不能是setText()的输入。 how to set text an integer and get int without getting error

1 个答案:

答案 0 :(得分:0)

您正在创建两个NumberPicker。因此,您在oncreate中执行的所有设置都会被新NumberPicker实例覆盖。

public void onClick(View view){
    Intent getPlayerNamesIntent = new Intent(this, AddPlayersActivity.class);

    // NumberPicker np = (NumberPicker)findViewById(R.id.np);
    // Integer numberOfPlayers = np.getValue();
    int numberOfPlayers = np.getValue();
    getPlayerNamesIntent.putExtra("NUM_PLAYERS", numberOfPlayers);

    startActivity(getPlayerNamesIntent);
}

您也不需要将textview设为最终版。

如果这不能解决您的问题,请告诉我。

修改 变量需要可供所有方法访问,因此在oncreate之外声明。然后在oncreate初始化。

public class MainActivity extends Activity {
    NumberPicker np;


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

        np = (NumberPicker) findViewById(R.id.np);

对不起我的坏话。我应该包括这个。