Android中的随机数

时间:2013-02-04 01:01:48

标签: android random switch-statement

当我点击我的测验游戏的开始按钮时,它会转到此活动。我使用这个类随机输入一个数字并自动调用另一个活动。我不知道什么是错的,但是当这个课程运行时,我的应用程序就会强行关闭。我认为这是因为随机..请帮助我..你能建议另一种随机数字方式吗?感谢..

package com.example.feedyourmind;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class LevelOne extends Activity {
    Integer button;
    ImageView iv;
    protected View view;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level_one);


        Intent myIntent = new Intent();

        Random rnd = new Random();
        int screen = (int) rnd.nextInt(10)+1;

        switch(screen){
        case 1:
            myIntent.setClass(this, Screen1.class);
            break;
        case 2:
            myIntent.setClass(this, Screen2.class);
            break;
        case 3:
            myIntent.setClass(this, Screen3.class);
            break;
        case 4:
            myIntent.setClass(this, Screen4.class);
            break;
        case 5:
            myIntent.setClass(this, Screen5.class);
            break;
        case 6:
            myIntent.setClass(this, Screen6.class);
            break;
        case 7:
            myIntent.setClass(this, Screen7.class);
            break;
        case 8:
            myIntent.setClass(this, Screen8.class);
            break;
        case 9:
            myIntent.setClass(this, Screen9.class);
            break;
        case 10:
            myIntent.setClass(this, Screen10.class);
            break;
        }
        startActivity(myIntent);
    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_level_one, menu);
        return true;
    }
  }

这是我的日志猫:

02-04 08:46:11.628: E/AndroidRuntime(388): FATAL EXCEPTION: main
02-04 08:46:11.628: E/AndroidRuntime(388): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.feedyourmind/com.example.feedyourmind.LevelOne}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.feedyourmind/com.example.feedyourmind.Screen9}; have you declared this activity in your AndroidManifest.xml?
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.os.Looper.loop(Looper.java:123)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-04 08:46:11.628: E/AndroidRuntime(388):  at java.lang.reflect.Method.invokeNative(Native Method)
02-04 08:46:11.628: E/AndroidRuntime(388):  at java.lang.reflect.Method.invoke(Method.java:507)
02-04 08:46:11.628: E/AndroidRuntime(388):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 08:46:11.628: E/AndroidRuntime(388):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 08:46:11.628: E/AndroidRuntime(388):  at dalvik.system.NativeStart.main(Native Method)
02-04 08:46:11.628: E/AndroidRuntime(388): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.feedyourmind/com.example.feedyourmind.Screen9}; have you declared this activity in your AndroidManifest.xml?
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.Activity.startActivityForResult(Activity.java:2827)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.Activity.startActivity(Activity.java:2933)
02-04 08:46:11.628: E/AndroidRuntime(388):  at com.example.feedyourmind.LevelOne.onCreate(LevelOne.java:64)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 08:46:11.628: E/AndroidRuntime(388):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-04 08:46:11.628: E/AndroidRuntime(388):  ... 11 more

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.feedyourmind"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.feedyourmind.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.feedyourmind.Menu"
            android:label="@string/title_activity_menu"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Menu1"
            android:label="@string/title_activity_menu1"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.HelpBg"
            android:label="@string/title_activity_help_bg"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.AboutUs"
            android:label="@string/title_activity_about_us"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Help"
            android:label="@string/title_activity_help"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.LevelOne"
            android:label="@string/title_activity_level_one"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen1"
            android:label="@string/title_activity_screen1"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen2"
            android:label="@string/title_activity_screen2"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen3"
            android:label="@string/title_activity_screen3"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen4"
            android:label="@string/title_activity_screen4"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen5"
            android:label="@string/title_activity_screen5"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen6"
            android:label="@string/title_activity_screen6"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen7"
            android:label="@string/title_activity_screen7"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen8"
            android:label="@string/title_activity_screen8"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen9"
            android:label="@string/title_activity_screen9"
            android:screenOrientation="landscape" >
        </activity>
        <activity
            android:name="com.example.feedyourmind.Screen10"
            android:label="@string/title_activity_screen10"
            android:screenOrientation="landscape" >
        </activity>
    </application>

</manifest>

答案 1 :(得分:0)

您应该在AndroidManifest.xml文件中声明您的活动。 整理所有活动,如

  <activity  android:name="com.example.feedyourmind.Screen1"/>

在mainifest文件中的应用程序标记内