2017-02-21 2 views
0

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' 
+0

die logcat Post –

+1

Instant Lauf versuchen zu deaktivieren, reinigen Sie die App, und führen Sie es erneut. – Rafa

+0

@RickS, Logcat hinzugefügt –

Antwort

2

Sie verwenden eine Endlosschleife in Ihrem Haupt-Thread. Dies macht es unmöglich, dass die Anwendung funktioniert. Betrachten Sie Ihre Uhr zu einem anderen Thread wie das Extrahieren:

public class TimerThread extends Thread { 
    boolean running; 

    int second; 
    int hours; 
    int minute; 
    int second_hand; 
    private Handler h; //Because when updating Views, you must do so from the mainThread 

    public TimerThread(int seconds) { 
     second = seconds; 
     running = true; 
     h = new Handler(); 
    } 

    protected void startTimer(){ 

     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); 

      handler.post(new Runnable() { 
        run() { 
         display.setText(time); //You might have to make display final 
        } 
       }); 


      try{ 
       Thread.sleep(1000); 
      }catch (Exception e){ 

      } 
     } 
    } 

    public void stopTimer(){ 
     running = false; 
    } 

    public int getSeconds() { 
     return second; 
    } 
} 

In Ihrer Tätigkeit:

private TimerThread mTimerThread; 
private int seconds; 
public void onCreate (...) { 
    ... 
    start_button.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       if (! running && mTimerThread == null) { 
        mTimerThread = new TimerThread(seconds); 
        mTimerThread.start(); 
       } 
      } 
     }); 

    pause_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (running && mTimerThread != null){ 
        mTimerThread.stopTimer(); 
        seconds = mTimerThread.getSeconds(); 
       } 
      } 
     }); 

    reset_button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mTimerThread != null) 
        mTimerThread.stopTimer(); 
       seconds = 0; 
       display.setText(""); 
       display.setHint("00:00:00"); 
      } 
     }); 
2

versuchen Sie den folgenden Code: Verwenden Sie einen Handler anstelle von Thread

public class MainActivity extends Activity { 

    private TextView textTimer; 
    private Button startButton; 
    private Button pauseButton; 
    private long startTime = 0L; 
    private Handler myHandler = new Handler(); 
    long timeInMillies = 0L; 
    long timeSwap = 0L; 
    long finalTime = 0L; 

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

     textTimer = (TextView) findViewById(R.id.textView2); 

     startButton = (Button) findViewById(R.id.start); 
     startButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       startTime = SystemClock.uptimeMillis(); 
       myHandler.postDelayed(updateTimerMethod, 0); 

      } 
     }); 

     pauseButton = (Button) findViewById(R.id.pause); 
     pauseButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       timeSwap += timeInMillies; 
       myHandler.removeCallbacks(updateTimerMethod); 

      } 
     }); 

    } 

    private Runnable updateTimerMethod = new Runnable() { 

     public void run() { 
      timeInMillies = SystemClock.uptimeMillis() - startTime; 
      finalTime = timeSwap + timeInMillies; 

      int seconds = (int) (finalTime/1000); 
      int minutes = seconds/60; 
      seconds = seconds % 60; 
      int milliseconds = (int) (finalTime % 1000); 
      textTimer.setText("" + minutes + ":" 
        + String.format("%02d", seconds) + ":" 
        + String.format("%03d", milliseconds)); 
      myHandler.postDelayed(this, 0); 
     } 

    }; 

} 
+0

Dank dieser Code funktioniert. aber ich möchte wissen, was war das Problem in meinem Code und warum Thread hier nicht funktioniert –

+0

Ihre While-Schleife nicht zu Ende .. – Hareesh

+0

Danke @Hareesh. Was ich brauche mit dieser While-Schleife, um es richtig zu machen. –

Verwandte Themen