2010-11-29 6 views
3

インテントから次のアクティビティにデータを渡して、それを受け取ってタイマー値を与えようとしています。インテントでデータを渡してそれを受け取る

Button TimerButton = (Button)findViewById(R.id.TimerActivityButton); 
TimerButton.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) 
    { 
      Intent timer = new Intent (BeefActivity.this,TimerActivity.class); 
      timer.putExtra("beefType", 5000); 
      timer.putExtra("beefThickness", 5000); 
      timer.putExtra("grillTime", 15000); 
      startActivity(timer); 
     } 
}); 

値を受け取るさまざまな方法を試しましたが、強制終了またはコンパイルエラーが続いています。私はこれが簡単なので、誰かが私にこれを行う方法を教えてください知っている。前もって感謝します!私はint型の長さ= beefType、それ力を渡してみました


This is force closing 
  • は私が変更しようとした-1 200に、それはまだ私が= 20000 int型の長さを元に戻す
  • を閉じて強制
  • を閉じました元々働いていた場所

私のクラスの先頭にあるint beefType = getIntent().getIntExtra("beefType", -1);は強制的に閉じるようにしています。コンパイラエラーはありません。フィールド初期化子として

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

のonCreateメソッドではありません:私はここに:-(が私のコードは、あなたがこの行を配置する必要があり


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

あなたが試したコードを投稿してください。 –

+0

また、エラーの瞬間にlogcat出力を取得してみてください – bbaja42

答えて

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

をどのように見えるかで立ち往生しています。

次回は、質問する前にスタックトレースを読んでください。読んだ後にまだスタックしている場合は、あなたの質問にも添付してください。

幸運!

+0

それについて申し訳ありません、私は次回、あなたに感謝します! – SickNick

関連する問題