我正在做一個簡單的秒錶應用程序。但在手機上打開時會崩潰。作爲簡單的Java應用程序單獨運行時,Java代碼運行良好。然而,實現在android系統與start
,pause
和reset
按鈕,它不工作這java代碼..簡單的定時器應用程序不工作在android
這裏是代碼...當
public class MainActivity extends AppCompatActivity {
private boolean running;
private int second;
private TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
running = false;
second = 0;
Button start_button = (Button)findViewById(R.id.start);
Button pause_button = (Button)findViewById(R.id.pause);
Button reset_button = (Button)findViewById(R.id.reset);
display = (TextView)findViewById(R.id.textView2);
start_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
running = true;
startTimer();
}
});
pause_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
running = false;
startTimer();
}
});
reset_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
running = false;
second = 0;
display.setText("");
display.setHint("00:00:00");
}
});
}
protected void startTimer(){
int hours;
int minute;
int second_hand;
while (running){
second++;
hours = second/3600;
minute = (second%3600)/60;
second_hand = second%60;
String time = String.format("%02d:%02d;%02d", hours, minute, second_hand);
display.setText(time);
try{
Thread.sleep(1000);
}catch (Exception e){
}
}
}
}
這裏是LogCat
:
02-22 00:03:18.207 13038-13038/? I/art: Late-enabling -Xcheck:jni
02-22 00:03:18.357 13038-13038/com.example.nishant.stopwatch I/InstantRun: Instant Run Runtime started. Android package is com.example.nishant.stopwatch, real application class is null.
02-22 00:03:18.447 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.447 13038-13038/com.example.nishant.stopwatch V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:18.457 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.457 13038-13038/com.example.nishant.stopwatch V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:18.767 13038-13038/com.example.nishant.stopwatch W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-22 00:03:18.817 13038-13038/com.example.nishant.stopwatch V/Monotype: SetAppTypeFace- try to flip, app = com.example.nishant.stopwatch
02-22 00:03:18.817 13038-13038/com.example.nishant.stopwatch V/Monotype: Typeface getFontPathFlipFont - systemFont = default#default
02-22 00:03:19.097 13038-13077/com.example.nishant.stopwatch D/OpenGLRenderer: Render dirty regions requested: true
02-22 00:03:19.107 13038-13038/com.example.nishant.stopwatch D/Atlas: Validating map...
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch I/OpenGLRenderer: Initialized EGL, version 1.4
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-22 00:03:19.187 13038-13077/com.example.nishant.stopwatch D/OpenGLRenderer: Enabling debug mode 0
02-22 00:04:46.007 13038-13047/com.example.nishant.stopwatch I/art: Thread[5,tid=13047,WaitingInMainSignalCatcherLoop,Thread*=0xb7ae7f30,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3
02-22 00:04:46.147 13038-13047/com.example.nishant.stopwatch I/art: Wrote stack traces to '/data/anr/traces.txt'
發佈logcat的 –
嘗試禁用即時運行,清理應用程序,並再次運行。 – Rafa
@RickS,Logcat加了 –