2012-07-02 3 views
0

2 개의 Android 활동을 통해 2 개의 변수를 전달하려고합니다. 그 중 하나는 마지막 페이지에 null를 높이 지 계속 :Android 인 텐트를 통해 변수를 전달할 때 NumberFormatException이 발생했습니다.

첫 번째 활동 :

두 번째 활동
Intent intent= new Intent(RoundOptionActivity.this, MoveOptionActivity.class); 
intent.putExtra("numRounds", "5"); 
startActivity(intent); 

:

Bundle extras = getIntent().getExtras(); 
if(extras !=null) { 
    numRounds = Integer.parseInt(extras.getString("numRounds")); 
} 

......... 

Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class); 
intent.putExtra("numRounds", numRounds); 
intent.putExtra("playerChoice", playerChoice); 
startActivity(intent); 

은 (주는이 시점에서 나는 로그 캣에 numRounds 인쇄

: 그것은 올바른 번호, NOT NULL)

에 세 번째 작업을 설정

이 시점에서 numRounds를 정수로 구문 분석하려고 시도하는 행에서 응용 프로그램이 충돌하고 null 값을 구문 분석 할 수 없다고 불평하는 NumberFormatException이 발생합니다. playerChoice에는 문제가 없으며 numRounds 만 있습니다. 나는 playerChoice와 똑같은 방식으로 numRounds를 처리하려고 시도했지만 아무것도 작동하지 않는 것 같습니다. 무슨 일이야? D :

답변

3

당신은이 extras.getInt("numRounds");

를 사용하는 두 번째 활동에 문자열로 값을 보내

numRounds = Integer.parseInt(extras.getString("numRounds")); 
+0

아! 나는 실제로 전에 그것을 시도했다. 그리고 그것은 그런 어떤 방법도 없다고 말했다. 또한 왜 2 번째 활동에서 int로 파싱했는지 모르겠지만 3 번째가 아니라 매우 이상합니다. 어쨌든, 그 덕분에, 고마워! =) –

2

사용

numRounds = extras.getInt("numRounds"); 

intead

numRounds = Integer.parseInt(extras.getString("numRounds")); 

의 당신이 두 번째 활동

에서 intent.putExtra("numRounds", numRounds);의 정수로 numRounds을 전달하거나로 받고 싶은 것처럼 통과하기 때문에 문자열 :

Intent intent = new Intent(MoveOptionActivity.this, MoveActivity.class); 
intent.putExtra("numRounds", numRounds+""); 
intent.putExtra("playerChoice", playerChoice); 
startActivity(intent); 
+0

그는'putExtra (String, String)'을 사용하여 시작했지만 ... –

+0

@AlexLockwood : this numRounds = Integer.parseInt (extras.getString ("numRounds")); –

+0

네,하지만 그는'intent.putExtra ("numRounds", "5");를 사용하여 처음에'String'을 저장했습니다. –

1

필자의 두 번째 활동에서 numRounds는 정수 변수 numRounds가 putExtra()에 정수 값을 설정하는 이유입니다. 즉 문제가되는 이유입니다. 중 하나로서 직접 extras.getInt("numRounds")처럼 세 번째 활동에서 numRounds을 얻거나 두 번째 Activity 당신이 putExtraINT 값에 추가하기 때문에, 즉 intent.putExtra("numRounds", numRounds+"");

관련 문제