2010-11-29 9 views
3

J'essaie de transmettre des données via mon intention dans l'activité suivante afin que je puisse la recevoir et lui donner des valeurs de minuterie. J'ai essayé différentes méthodes de réception des valeurs, et je continue à obtenir une force de fermeture ou des erreurs de compilation. Je sais que c'est simple, alors quelqu'un peut me montrer comment faire. Merci d'avance!Passage de données via Intention et réception


This is force closing 
  • I essayé passant de longueur int = beefType et la force fermée
  • I essayé de changer -1 à 200, il forcer encore fermé
  • Je remis longueur int = 20000 où il a fonctionné à l'origine

Juste en ayant int beefType = getIntent().getIntExtra("beefType", -1); au sommet de ma classe le force à fermer. Aucune erreur de compilation. Je suis coincé :-(Voici comment mon code ressemble


package com.android.project1; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class TimerActivity extends Activity { 

    int beefType = getIntent().getIntExtra("beefType", 200); 

    TextView timeDisplay; 
    MyCount counter; 
    int state = 0; 
    int length = 20000; 
    long startTime = 0; 
    long currentTime = 0; 
    long timeElapsed = 0; 
    long timeRemaining = 0; 
    long prevTimeRemaining = 0; 
    Button control; 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.timer); 

    timeDisplay = (TextView) findViewById(R.id.timer); 
    control = (Button) findViewById(R.id.control); 
    counter = new MyCount(length, 100); 
    } 
    public String formatTime(long millis) 
    { 
     String output = "00:00:00"; 
     long seconds = millis/1000; 
     long minutes = seconds/60; 
     long hours = minutes/60; 

     seconds = seconds % 60; 
     minutes = minutes % 60; 
     hours = hours % 60; 

     String secondsD = String.valueOf(seconds); 
     String minutesD = String.valueOf(minutes); 
     String hoursD = String.valueOf(hours); 

     if (seconds < 10) 
     secondsD = "0" + seconds; 
     if (minutes < 10) 
     minutesD = "0" + minutes; 
     if (hours < 10) 
     hoursD = "0" + hours; 

     output = hoursD + " : " + minutesD + " : " + secondsD; 
     return output; 
    } 
    public void control(View view) { 
    switch (state) { 
    case 0: 
     startTime = System.currentTimeMillis(); 
     counter.start(); 
     control.setText(R.string.pause); 
     state = 1; 
     break; 
    case 1: 
     // Pause 
     currentTime = System.currentTimeMillis(); 
     timeElapsed = currentTime - startTime; 
     if (prevTimeRemaining == 0) 
     timeRemaining = length - timeElapsed; 
     else 
     timeRemaining = prevTimeRemaining - timeElapsed; 
     counter.cancel(); 
     timeDisplay.setText("Left: " + String.valueOf(formatTime(timeRemaining))); 
     control.setText(R.string.resume); 
     prevTimeRemaining = timeRemaining; 

     // Resume 
     counter = new MyCount(timeRemaining, 100); 
     state = 0; 
     break; 
    case 2: 
     prevTimeRemaining = 0; 
     counter = new MyCount(length, 100); 
     control.setText(R.string.start); 
     timeDisplay.setText(R.string.timer); 
     state = 0; 
    } 
    } 

    public class MyCount extends CountDownTimer 
    { 

    public MyCount(long millisInFuture, long countDownInterval) 
    { 
     super(millisInFuture, countDownInterval); 
    } 
    public void onTick(long timeRemaining) 
    { 
     timeDisplay.setText("Left: " + formatTime(timeRemaining)); 
    } 
    public void onFinish() 
    { 
     timeDisplay.setText("Finished!"); 
     state = 2; 
     control.setText(R.string.restart); 
    } 
    } 
} 
+1

Postez le code que vous avez essayé. –

+0

aussi, essayez d'obtenir la sortie logcat au moment de l'erreur – bbaja42

Répondre

6
int beefType = getIntent().getIntExtra("beefType", -1); 
1

Vous devez mettre cette ligne:

int beefType = getIntent().getIntExtra("beefType", 200); 

dans la méthode onCreate et non comme un initialiseur sur le terrain.

Et la prochaine fois - lisez la pile avant de la poser.Si vous êtes toujours coincé après l'avoir lu, attachez-le aussi à la question de votre jeu.

Bonne chance!

+0

Désolé à ce sujet, je vais la prochaine fois, merci les gars! – SickNick

Questions connexes