我想制作七个数字幸运抽奖的应用程序。这是我的代码。但我不知道为什么当我不止一次点击按钮时应用会停止。单击 CLEAR 后, NEXT NUMBER 按钮没有响应。
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:&gt;&gt;&gt;&gt;&gt;&gt; START
com.android.internal.os.RuntimeInit uid 0&lt;&lt;&lt;&lt;&lt;&lt;&lt; 10-26 13:11:12.690
3289-3289 /? D / AndroidRuntime:CheckJNI开启10-26 13:11:12.704
3289-3289 /? I / art:使用code_cache_capacity = 2MB创建的JIT compile_threshold = 1000 10-26 13:11:12.707 3289-3289 /? D / ICU:没有 找到时区覆盖文件: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 10-26 13:11:12.742
3289-3289 /? E / memtrack:无法加载memtrack模块(没有这样的文件或 目录)10-26 13:11:12.742 3289-3289 /? E / android.os.Debug: 无法加载memtrack模块:-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:调用主条目 com.android.commands.am.Am 10-26 13:11:12.761 1296-1433 /? I / ActivityManager:强制停止com.example.natalie.myapplication appid = 10058 user = 0:from pid 3289 10-26 13:11:12.762 1296-1433 /? I / ActivityManager:杀戮 3267:com.example.natalie.myapplication / u0a58(adj 0):停止 com.example.natalie.myapplication 10-26 13:11:12.771 1296-1308 /? D / GraphicsStats:缓冲计数: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:强制删除 ActivityRecord {23731b7 u0 com.example.natalie.myapplication / .MainActivity t41}:app死了,没有 保存状态10-26 13:11:12.866 1296-1311 /? W / art:长显示器 所有者方法= void的争用事件 com.android.server.am.ActivityManagerService.forceStopPackage(java.lang.String中, int)来自ActivityManagerService.java:5254 waiters = 0 for 102ms 10-26 13:11:12.866 1296-1703 /? W / ActivityManager:虚假死亡 ProcessRecord {c82593e 0:com.example.natalie.myapplication / u0a58}, curProc for 3267:null 10-26 13:11:12.879 3289-3289 /? D / AndroidRuntime:关闭VM 10-26 13:11:12.921 1544-1807 /? W / EGL_emulation:eglSurfaceAttrib未实现10-26 13:11:12.921
1544至1807年/? W / OpenGLRenderer:无法设置EGL_SWAP_BEHAVIOR 表面0xa36b1ce0,错误= EGL_SUCCESS 10-26 13:11:13.627
1296年至1703年/? W / InputMethodManagerService:发送了RemoteException setActive(false)通知pid 3267 uid 10058 10-26 13:11:13.630 1544年至1544年/? I / Choreographer:跳过40帧!申请可能 在主线程上做太多工作。 10-26 13:11:13.854
3302-3302 /? D / AndroidRuntime:&gt;&gt;&gt;&gt;&gt;&gt;开始 com.android.internal.os.RuntimeInit uid 0&lt;&lt;&lt;&lt;&lt;&lt;&lt; 10-26 13:11:13.855
3302-3302 /? D / AndroidRuntime:CheckJNI开启10-26 13:11:13.873
3302-3302 /? I / art:使用code_cache_capacity = 2MB创建的JIT compile_threshold = 1000 10-26 13:11:13.875 3302-3302 /? D / ICU:没有 找到时区覆盖文件: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 10-26 13:11:13.894
3302-3302 /? E / memtrack:无法加载memtrack模块(没有这样的文件或 目录)10-26 13:11:13.894 3302-3302 /? E / android.os.Debug: 无法加载memtrack模块:-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:调用主条目 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 来自uid 0的cmp = com.example.natalie.myapplication / .MainActivity} 显示0 10-26 13:11:13.931 3302-3302 /? D / AndroidRuntime:关机 VM 10-26 13:11:13.962 1296-2483 /? I / ActivityManager:开始 proc 3312:com.example.natalie.myapplication / u0a58用于活动 com.example.natalie.myapplication / .MainActivity 10-26 13:11:13.965
3312-3312 /?我/艺术:不迟到-Xcheck:jni(已经开启)10-26 13:11:13.966 3312-3312 /?我/艺术:延迟启用JIT 10-26 13:11:14.017 1296-1306 /?我/艺术:背景粘性并发标记 扫描GC释放39389(2MB)AllocSpace对象,12(292KB)LOS对象, 31%免费,7MB / 10MB,暂停1.682ms总计102.189ms 10-26 13:11:14.037 3312-3312 /? I / art:使用code_cache_capacity = 2MB创建的JIT compile_threshold = 1000 10-26 13:11:14.061 3312-3312 /? W /系统: ClassLoader引用了未知路径: /data/app/com.example.natalie.myapplication-2/lib/x86 10-26 13:11:14.261 1544-1807 /? W / OpenGLRenderer:错误地调用 ViewLayer on View:ShortcutAndWidgetContainer,破坏图层...... 10-26 13:11:14.261 1544-1807 /? W / OpenGLRenderer:错误地调用 ViewLayer on View:ShortcutAndWidgetContainer,破坏图层...... 10-26 13:11:14.274 1544-1807 /? E / Surface:getSlotFromBufferLocked: 未知缓冲区:0xa21ce2d0 10-26 13:11:14.314 954-1353 /? E / SurfaceFlinger:ro.sf.lcd_density必须定义为构建 物业10-26 13:11:14.315 3312-3327 /? D / OpenGLRenderer:使用 EGL_SWAP_BEHAVIOR_PRESERVED:true 10-26 13:11:14.322 3312-3312 /? D /:HostConnection :: get()建立新主机连接0xad778fe0, tid 3312 10-26 13:11:14.375 3312-3327 /? D /:HostConnection :: get() 新主机连接建立0xaf87f0b0,tid 3327 10-26 13:11:14.383 3312-3327 /? I / OpenGLRenderer:初始化的EGL,版本 1.4 10-26 13:11:14.454 3312-3327 /? W / EGL_emulation:eglSurfaceAttrib未实现10-26 13:11:14.454 3312-3327 /? W / OpenGLRenderer:无法在曲面上设置EGL_SWAP_BEHAVIOR 0xabf7f7c0,错误= EGL_SUCCESS 10-26 13:11:15.014 1296-1315 /? I / ActivityManager:显示 com.example.natalie.myapplication / .MainActivity:+ 1s55ms 10-26 13:11:41.433 1531-1542 /?我/艺术:背景粘性并发标记 扫描GC释放12987(821KB)AllocSpace对象,0(0B)LOS对象,44% 免费,1278KB / 2MB,暂停6.254ms总计33.275ms 10-26 13:11:48.392
965-1288 /? D / AudioFlinger:调音台(0xb4480000)节气门结束:油门 时间(410)10-26 13:11:52.492 965-1288 /? d / AudioFlinger: 混合器(0xb4480000)节流结束:节流时间(18)10-26 13:11:54.003
965-1288 /? D / AudioFlinger:调音台(0xb4480000)节气门结束:油门 时间(14)
答案 0 :(得分:0)
我可以看到arr
数组中的最后一个初始化元素是第五个:
arr[5] = (TextView)findViewById(R.id.tv_number_six);
但你确实调用了arr
中的第六个元素:
clear(arr[6]);
这是否是NullPointerException?
答案 1 :(得分:0)
{
错误之后,在您的交换机块中有一个case:
试试这个:
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++;