-1

I want to make an app of Lucky Draw of seven numbers.Here is my code. But I don't know why the app will stop when I click the buttons more than once. And after I click CLEAR, the NEXT NUMBER button has no response.

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView[] arr = new TextView[7];
int[] num = new int[6];
int count = 0;
TextView number_7;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    arr[0] = ((TextView) findViewById(R.id.tv_number_one));
    arr[1] = ((TextView) findViewById(R.id.tv_number_two));
    arr[2] = (TextView)findViewById(R.id.tv_number_three);
    arr[3] = (TextView)findViewById(R.id.tv_number_four);
    arr[4] = (TextView)findViewById(R.id.tv_number_five);
    arr[5] = (TextView)findViewById(R.id.tv_number_six);
    number_7 = (TextView)findViewById(R.id.tv_number_seven);

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void generate(View v)
{
    Random myRandom = new Random();
    if (count >= 7) {
        clear(arr[0]);
        clear(arr[1]);
        clear(arr[2]);
        clear(arr[3]);
        clear(arr[4]);
        clear(arr[5]);
        clear(arr[6]);
        for (TextView tv : arr) tv.setText("?");
    }
    switch (count){
        case(0):{
            num[0]= myRandom.nextInt(48) + 1;
            arr[0].setText(String.valueOf(num[0]));
            break;
        }
        case(1):{
            num[1]= myRandom.nextInt(48) + 1;
            arr[1].setText(String.valueOf(num[1]));
            break;
        }
        case(2):{
            num[2]= myRandom.nextInt(48) + 1;
            arr[2].setText(String.valueOf(num[2]));
            break;
        }
        case(3):{
            num[3]= myRandom.nextInt(48) + 1;
            arr[3].setText(String.valueOf(num[3]));
            break;
        }
        case(4):{
            num[4]= myRandom.nextInt(48) + 1;
            arr[4].setText(String.valueOf(num[4]));
            break;
        }
        case(5):{
            num[5]= myRandom.nextInt(48) + 1;
            arr[5].setText(String.valueOf(num[5]));
            break;
        }
        case(6):{
            int num_7;
            Arrays.sort(num);
            num_7= myRandom.nextInt(48) + 1;
            arr[0].setText(String.valueOf(num[0]));
            arr[1].setText(String.valueOf(num[1]));
            arr[2].setText(String.valueOf(num[2]));
            arr[3].setText(String.valueOf(num[3]));
            arr[4].setText(String.valueOf(num[4]));
            arr[5].setText(String.valueOf(num[5]));
            number_7.setText(String.valueOf(num_7));
            break;
        }

    }
    count++;


}
public void clear(View v){
    TextView num_1 = (TextView)findViewById(R.id.tv_number_one);
    num_1.setText("?");
    TextView num_2 = (TextView)findViewById(R.id.tv_number_two);
    num_2.setText("?");
    TextView num_3 = (TextView)findViewById(R.id.tv_number_three);
    num_3.setText("?");
    TextView num_4 = (TextView)findViewById(R.id.tv_number_four);
    num_4.setText("?");
    TextView num_5 = (TextView)findViewById(R.id.tv_number_five);
    num_5.setText("?");
    TextView num_6 = (TextView)findViewById(R.id.tv_number_six);
    num_6.setText("?");
    TextView num_7 = (TextView)findViewById(R.id.tv_number_seven);
    num_7.setText("?");
}
}

activity_main.xml

 <TextView android:text="\?" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_number_one"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="10dp"
    android:textColor="#67ceff"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_two"
    android:layout_alignBottom="@+id/tv_number_one"
    android:layout_toRightOf="@+id/tv_number_one"
    android:layout_toEndOf="@+id/tv_number_one"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_three"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_two"
    android:layout_toEndOf="@+id/tv_number_two"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_four"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_three"
    android:layout_toEndOf="@+id/tv_number_three"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:textIsSelectable="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_five"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_four"
    android:layout_toEndOf="@+id/tv_number_four"
    android:layout_marginLeft="20dp"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_six"
    android:layout_alignBottom="@+id/tv_number_five"
    android:layout_toRightOf="@+id/tv_number_five"
    android:layout_toEndOf="@+id/tv_number_five"
    android:textColor="#67ceff"
    android:textSize="30sp"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="\?"
    android:id="@+id/tv_number_seven"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="20dp"
    android:textColor="#6198ff"
    android:textSize="30sp"
    android:layout_marginStart="20dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NEXT NUMBER"
    android:id="@+id/button_next_num"
    android:layout_below="@+id/tv_number_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="41dp"
    android:onClick="generate"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CLEAR"
    android:id="@+id/button_clear"
    android:onClick="clear"
    android:layout_alignTop="@+id/button_next_num"
    android:layout_toRightOf="@+id/tv_number_six"
    android:layout_toEndOf="@+id/tv_number_six"
    android:layout_marginLeft="30dp" />

Logcat

10-26 13:11:12.688 3289-3289/? D/AndroidRuntime﹕ >>>>>> START
com.android.internal.os.RuntimeInit uid 0 <<<<<< 10-26 13:11:12.690
3289-3289/? D/AndroidRuntime﹕ CheckJNI is ON 10-26 13:11:12.704
3289-3289/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000 10-26 13:11:12.707 3289-3289/? D/ICU﹕ No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 10-26 13:11:12.742
3289-3289/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory) 10-26 13:11:12.742 3289-3289/? E/android.os.Debug﹕ failed to load memtrack module: -2 10-26 13:11:12.744 3289-3289/? I/Radio-JNI﹕ register_android_hardware_Radio DONE 10-26 13:11:12.758
3289-3289/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am 10-26 13:11:12.761 1296-1433/? I/ActivityManager﹕ Force stopping com.example.natalie.myapplication appid=10058 user=0: from pid 3289 10-26 13:11:12.762 1296-1433/? I/ActivityManager﹕ Killing 3267:com.example.natalie.myapplication/u0a58 (adj 0): stop com.example.natalie.myapplication 10-26 13:11:12.771 1296-1308/? D/GraphicsStats﹕ Buffer count: 3 10-26 13:11:12.771 1296-1308/? I/WindowState﹕ WIN DEATH: Window{71edfc1 u0 com.example.natalie.myapplication/com.example.natalie.myapplication.MainActivity} 10-26 13:11:12.848 1296-1433/? W/ActivityManager﹕ Force removing ActivityRecord{23731b7 u0 com.example.natalie.myapplication/.MainActivity t41}: app died, no saved state 10-26 13:11:12.866 1296-1311/? W/art﹕ Long monitor contention event with owner method=void com.android.server.am.ActivityManagerService.forceStopPackage(java.lang.String, int) from ActivityManagerService.java:5254 waiters=0 for 102ms 10-26 13:11:12.866 1296-1703/? W/ActivityManager﹕ Spurious death for ProcessRecord{c82593e 0:com.example.natalie.myapplication/u0a58}, curProc for 3267: null 10-26 13:11:12.879 3289-3289/? D/AndroidRuntime﹕ Shutting down VM 10-26 13:11:12.921 1544-1807/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented 10-26 13:11:12.921
1544-1807/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa36b1ce0, error=EGL_SUCCESS 10-26 13:11:13.627
1296-1703/? W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 3267 uid 10058 10-26 13:11:13.630 1544-1544/? I/Choreographer﹕ Skipped 40 frames! The application may be doing too much work on its main thread. 10-26 13:11:13.854
3302-3302/? D/AndroidRuntime﹕ >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 10-26 13:11:13.855
3302-3302/? D/AndroidRuntime﹕ CheckJNI is ON 10-26 13:11:13.873
3302-3302/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000 10-26 13:11:13.875 3302-3302/? D/ICU﹕ No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 10-26 13:11:13.894
3302-3302/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory) 10-26 13:11:13.894 3302-3302/? E/android.os.Debug﹕ failed to load memtrack module: -2 10-26 13:11:13.895 3302-3302/? I/Radio-JNI﹕ register_android_hardware_Radio DONE 10-26 13:11:13.905
3302-3302/? D/AndroidRuntime﹕ Calling main entry com.android.commands.am.Am 10-26 13:11:13.908 1296-1308/? I/ActivityManager﹕ START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.natalie.myapplication/.MainActivity} from uid 0 on display 0 10-26 13:11:13.931 3302-3302/? D/AndroidRuntime﹕ Shutting down VM 10-26 13:11:13.962 1296-2483/? I/ActivityManager﹕ Start proc 3312:com.example.natalie.myapplication/u0a58 for activity com.example.natalie.myapplication/.MainActivity 10-26 13:11:13.965
3312-3312/? I/art﹕ Not late-enabling -Xcheck:jni (already on) 10-26 13:11:13.966 3312-3312/? I/art﹕ Late-enabling JIT 10-26 13:11:14.017 1296-1306/? I/art﹕ Background sticky concurrent mark sweep GC freed 39389(2MB) AllocSpace objects, 12(292KB) LOS objects, 31% free, 7MB/10MB, paused 1.682ms total 102.189ms 10-26 13:11:14.037 3312-3312/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000 10-26 13:11:14.061 3312-3312/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.natalie.myapplication-2/lib/x86 10-26 13:11:14.261 1544-1807/? W/OpenGLRenderer﹕ Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer... 10-26 13:11:14.261 1544-1807/? W/OpenGLRenderer﹕ Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer... 10-26 13:11:14.274 1544-1807/? E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xa21ce2d0 10-26 13:11:14.314 954-1353/? E/SurfaceFlinger﹕ ro.sf.lcd_density must be defined as a build property 10-26 13:11:14.315 3312-3327/? D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true 10-26 13:11:14.322 3312-3312/? D/﹕ HostConnection::get() New Host Connection established 0xad778fe0, tid 3312 10-26 13:11:14.375 3312-3327/? D/﹕ HostConnection::get() New Host Connection established 0xaf87f0b0, tid 3327 10-26 13:11:14.383 3312-3327/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4 10-26 13:11:14.454 3312-3327/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented 10-26 13:11:14.454 3312-3327/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf7f7c0, error=EGL_SUCCESS 10-26 13:11:15.014 1296-1315/? I/ActivityManager﹕ Displayed com.example.natalie.myapplication/.MainActivity: +1s55ms 10-26 13:11:41.433 1531-1542/? I/art﹕ Background sticky concurrent mark sweep GC freed 12987(821KB) AllocSpace objects, 0(0B) LOS objects, 44% free, 1278KB/2MB, paused 6.254ms total 33.275ms 10-26 13:11:48.392
965-1288/? D/AudioFlinger﹕ mixer(0xb4480000) throttle end: throttle time(410) 10-26 13:11:52.492 965-1288/? D/AudioFlinger﹕ mixer(0xb4480000) throttle end: throttle time(18) 10-26 13:11:54.003
965-1288/? D/AudioFlinger﹕ mixer(0xb4480000) throttle end: throttle time(14)

Fakher
  • 2,098
  • 3
  • 29
  • 45
Joey Li
  • 45
  • 5
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Selvin Oct 26 '15 at 12:33
  • @Selvin Fortunately it has not stopped now. But I don't understand why the "NEXT BUTTON" has no reaction after I click "CLEAR" – Joey Li Oct 26 '15 at 12:38
  • What is the `OnClickListener` for the buttons? – shhp Oct 26 '15 at 12:47
  • @shhp Fortunately it has not stopped now. But I don't understand why the "NEXT BUTTON" has no reaction after I click "CLEAR". Can you have a look of Logcat message? – Joey Li Oct 26 '15 at 13:18
  • i think that ur problem is not in ur code !! there is no reference of an exception to your code in the log !! – Fakher Oct 26 '15 at 13:44
  • @Fakher What can I do? – Joey Li Oct 26 '15 at 13:46
  • does the application show you the buttons and the view or not ?? – Fakher Oct 26 '15 at 13:47
  • Please post the logcat of only AndroidRuntime and System.err ( adb logcat *:s AndroidRuntime:v System.err:v ) – Shark Oct 26 '15 at 14:05
  • Add click Listeners to the Buttons otherwise they won't detect the click event form the Main UI thread – Fred Ondieki Oct 26 '15 at 14:25
  • @Fakher When I run the apps, it show the button. I can click the "NEXT BUTTON" button to generate a new number. However, after I press "CLEAR", the "NEXT NUMBER" button have no reaction. – Joey Li Oct 26 '15 at 14:35
  • @Shark I don't understand which part. I just post the logcat between the time of pressing Run and the time of pressing CLEAR – Joey Li Oct 26 '15 at 14:38
  • @JoeyLi I just told you to post a relevant piece of the logcat. AndroidRuntime and System.err logtags will capture the reason and the source of the crash, so you can clarify the question a bit better. – Shark Oct 26 '15 at 15:00

1 Answers1

0

in your switch bloc there is a { after each case: wich is wrong try with this:

switch (count){
        case(0):
            num[0]= myRandom.nextInt(48) + 1;
            arr[0].setText(String.valueOf(num[0]));
            break;

        case(1):
            num[1]= myRandom.nextInt(48) + 1;
            arr[1].setText(String.valueOf(num[1]));
            break;

        case(2):
            num[2]= myRandom.nextInt(48) + 1;
            arr[2].setText(String.valueOf(num[2]));
            break;

        case(3):
            num[3]= myRandom.nextInt(48) + 1;
            arr[3].setText(String.valueOf(num[3]));
            break;

        case(4):
            num[4]= myRandom.nextInt(48) + 1;
            arr[4].setText(String.valueOf(num[4]));
            break;

        case(5):
            num[5]= myRandom.nextInt(48) + 1;
            arr[5].setText(String.valueOf(num[5]));
            break;

        case(6):
            int num_7;
            Arrays.sort(num);
            num_7= myRandom.nextInt(48) + 1;
            arr[0].setText(String.valueOf(num[0]));
            arr[1].setText(String.valueOf(num[1]));
            arr[2].setText(String.valueOf(num[2]));
            arr[3].setText(String.valueOf(num[3]));
            arr[4].setText(String.valueOf(num[4]));
            arr[5].setText(String.valueOf(num[5]));
            number_7.setText(String.valueOf(num_7));
            break;


    }
    count++;
Fakher
  • 2,098
  • 3
  • 29
  • 45
  • Thank you. Corrected. But the "NEXT NUMBER" still cannot work after pressing the "CLEAR"button. – Joey Li Oct 26 '15 at 14:16
  • tell me what do you expect and wht do u get so i can help u – Fakher Oct 26 '15 at 14:31
  • I can click the "NEXT BUTTON" button to generate a new number. However, after I press "CLEAR", the "NEXT NUMBER" button have no reaction. But I want after pressing "CLEAR", I can click the "NEXT NUMBER" button to do the random again. – Joey Li Oct 26 '15 at 14:42
  • can you add logs after each case sohwing the random number generated ? – Fakher Oct 26 '15 at 14:54