android的乐透程序

时间:2012-11-15 20:10:43

标签: android arrays

好的,我正在为我的班级制作一个乐透程序。当我点击生成它崩溃时,我不知道当你点击生成时我是否为数组做了正确的编码。我是初学者,但努力做得更好。 提前谢谢。

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();
        }   
        }       
        });  
   }
}

logcat的

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)

1 个答案:

答案 0 :(得分:0)

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

numbers1.setText(al.get(x + ""));