0

好的,所以我为我的班级制作了一个乐透计划。当我单击生成时它崩溃,我不知道当您单击生成时我是否对数组进行了正确的编码。我是初学者,但正在努力变得更好。提前致谢。

public class MainActivity extends Activity {
    EditText min,max;
    TextView numbers1;

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

        setContentView(R.layout.activity_main);
        numbers1 = (TextView) findViewById(R.id.numbers);
        max = (EditText) findViewById(R.id.maxinput);
        min = (EditText) findViewById(R.id.mininput);

        ImageButton lotto = (ImageButton) findViewById(R.id.button1);
        lotto.setOnClickListener(new View.OnClickListener() {

            @Override
        public void onClick(View v) {
        ArrayList<Integer> al = new ArrayList<Integer>();
        for(int i = Integer.parseInt(min.getText().toString()); i <= Integer.parseInt(max.getText().toString()); i++)

            al.add(i);

        Random ran = new Random();

        for(int i = 0; i < 5; i++) {
            int x = al.remove(ran.nextInt(al.size()));
            al.get(x).toString();
        }   
        }       
        });  
   }
}

日志猫

11-15 12:19:46.562: E/AndroidRuntime(31065): FATAL EXCEPTION: main
11-15 12:19:46.562: E/AndroidRuntime(31065): java.lang.IllegalArgumentException
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
java.util.Random.nextInt(Random.java:187)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
com.edu.ext.lottoapp.MainActivity$1.onClick(MainActivity.java:42)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
android.view.View.performClick(View.java:4084)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
android.view.View$PerformClick.run(View.java:16966)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at    
android.os.Handler.handleCallback(Handler.java:615)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
android.os.Handler.dispatchMessage(Handler.java:92)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at    
android.os.Looper.loop(Looper.java:137)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
android.app.ActivityThread.main(ActivityThread.java:4745)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
java.lang.reflect.Method.invokeNative(Native Method)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
java.lang.reflect.Method.invoke(Method.java:511)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-15 12:19:46.562: E/AndroidRuntime(31065):    at 
dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您不能将整数设置为 TextView。做这样的事情:

numbers1.setText(al.get(x + ""));
于 2012-11-15T20:13:12.473 回答