2013-07-19 2 views
3

나는 안드로이드 프로그래밍에 상당히 익숙하다. 그리고 나는이 문제가있다 .. 나는 가치를 전달하고 싶지만, 결과는 null이다. 나는 왜 그런가 ... 누구든지 나를 도울 수 있을까? 미리 감사드립니다. 내 코드는 ...이 같은 것입니다Android 전달 값 왜 null입니까?

Manager.java

String prize="5"; 
Intent i = new Intent(Manager.this, Shop.class); 
i.putExtra("Key", prize); 
startActivity(i); 

Shop.java

Intent myIntent = getIntent(); 
    String receive = myIntent.getStringExtra("Key"); 

    if (getIntent().getExtras() != null) 
    {    
    TextView tv = (TextView)findViewById(R.id.textView2); 
    tv.setText(receive); 
    } 

    else 
    { 
     TextView tv = (TextView)findViewById(R.id.textView2); 
     tv.setText("value is null"); //this is always the result 
             //why is it null?? 
    } 
+0

먼저 이것을 사용하여 데이터의 유무를 확인하십시오. Toast.makeText (Shop.is, "+ 수신, Toast.LENGTH_LONG) .show(); – TheLittleNaruto

+0

@ 쿠마 Toast.makeText 시도했지만 값이 아직 null입니다 – user2599219

+0

안녕하세요.이 줄 바로 아래 토스트하십시오. String receive = myIntent.getStringExtra ("Key"); 조건에 맞지 않습니다. – TheLittleNaruto

답변

1

함수 getExtras()를 반환 putExtras를 사용하여 의도에 배치 Bundle (B). 이런 식으로 뭔가 :.

Intent i = new Intent(Manager.this, Shop.class); 
i.putExtras(new Bundle()); 
startActivity(i); 

당신이 putExtras 기능 다음 getIntent() getExtras를 (사용하지 않기 때문에)이 null을 반환합니다. 이처럼 수행해야합니다

Intent myIntent = getIntent(); 
String receive = myIntent.getStringExtra("Key"); 

if (receive != null) 
{    
TextView tv = (TextView)findViewById(R.id.textView2); 
tv.setText(receive); 
} 

else 
{ 
    TextView tv = (TextView)findViewById(R.id.textView2); 
    tv.setText("value is null"); //this is always the result 
            //why is it null?? 
} 
1

당신이

String value= "Your String"; 
// Launching new Activity on selecting single List Item 
Intent i = new Intent(getApplicationContext(), Test.class); 
// sending data to new activity 
i.putExtra("key", value); 
startActivity(i); 

를 설정하고 다음 활동에

Intent i = getIntent(); 
// getting attached intent data 
String value= i.getStringExtra("value"); 
// displaying selected product name 
txtProduct.setText(value); 
+0

나는 이것을 시도했습니다. 하지만 아무것도 내 textview 일이. 토스트를 추가했는데 값이 null입니다. 왜 그렇게 생각하니? – user2599219

+0

@ user2599219이 url을 보았습니다 http://www.androidhive.info/2011/10/android-listview-tutorial/ –

+0

조차도이 URL에서 내 문제의 해결책을 얻었습니다. –

1

이 시도 어떤 다른 키에 액세스하려고하는이 시도 :

getIntent().getExtras().getString("KEY"); 

5 월 예정 그것을해야합니다.