Ich mache eine einfache Stoppuhr-Anwendung. Aber es stürzt beim Öffnen auf dem Handy ab. Java-Code funktioniert gut, wenn er einzeln als einfache Java-Anwendung ausgeführt wird. Wenn jedoch diese Java-Code in Android-Implementierung mit start
, pause
und reset
Taste, seine nicht funktioniert ..einfache Timer-Anwendung funktioniert nicht in android
Hier ist der Code ...
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){
}
}
}
}
Hier ist die 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'
die logcat Post –
Instant Lauf versuchen zu deaktivieren, reinigen Sie die App, und führen Sie es erneut. – Rafa
@RickS, Logcat hinzugefügt –