2011-05-10 5 views
28

두 가지 활동간에 문자열을 전달하려고합니다. 같은 방법으로 다른 프로젝트에서이 작업을 수행했지만 intent.getStringExtra (String)을 호출 할 때 어떤 이유로 NullPointerException이 발생합니다. 또한Android getIntent(). getExtras()가 null을 반환합니다.

을 통해 번들을 만들려고했으나 null도 반환했습니다. 아래는 현재 사용하려는 코드입니다.

활동 A :

나는 엑스트라를 전달하는 모든 다른 방법 꽤 많이 시도했습니다
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Intent i = getIntent(); 

    if (i == null) 
     Log.d("***DEBUG****", "Intent was null"); 
    else 
     Log.d("**** DEBUG ***", "Intent OK"); 

    String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line 
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID); 

,하지만 그들은 모두 : 여기

Intent myIntent = null; 
    String select = ""; 
      if (selection.equals("Chandelle")) { 
       myIntent = new Intent(Commercial.this, Chandelle.class); 
       select = "Chandelle"; 
      } else if (selection.equals("Eights on Pylons")) { 
       myIntent = new Intent(Commercial.this, EightsOnPylons.class); 
       select = "Eights on Pylons"; 
      } 
// Start the activity 
    if (myIntent != null) { 
     myIntent.putExtra("selection", select); 
     Log.d("*** OUTBOUND INTENT: ", "" + myIntent.getExtras().get("selection")); 
     startActivity(myIntent); 
    } 

여분을 가져 오는 활동 B의 코드입니다 이런 식으로 행동하는 것 같습니다. 내가 뭘 놓치고 있니?

+0

이 문제를 어떻게 해결 했습니까? – aProgrammer

+0

다른 해결책 : getExtras(). get ("sharedString") – fungusanthrax

답변

1

코드를 살펴보면 활동 1에 어떤 것을 넣었을 때 일어날 수 있다고 생각합니다. "if ... else if"를 사용하고 다른 경우에는 "else"를 사용하지 않기 때문입니다. 그럴 경우 활동 2에서 널 값을 가져옵니다.이 경우 다시 확인하십시오.

+1

행운을 빈다. 내가 게시 한 활동 B가 실제로 EightsOnPylons.java라는 점에서 좀 더 구체적이어야했습니다. 또한, 로그."OUTBOUND INTENT"를 인쇄하는 활동 1의 d 문은 올바른 문자열을 표시하므로 여분이 실제로 의도와 함께 전송되고 있음을 확신합니다. – FlapsFail

+8

문제점을 발견했습니다. 코드가 아닌 프로그램의 디자인에 문제가있었습니다. B 액티비티는 C 액티비티라는 탭 호스트였습니다. 액티비티 C에서 추가로 가져 오려고했지만 여분은 A에서 C로만 전송되었습니다. – FlapsFail

7

운 좋게도이 글을 발견했습니다. 나는 이것을 몇 시간 동안 고민해 왔고, 결국 나는 내 의도를 tabHost에 보내고 있다는 것을 깨달았다. ;-) 여전히 문제가있는 사람들은 tabHost 활동 자체에서 엑스트라를 가져 와서 하위 탭으로 전달하십시오.

String userID; 

. .

Bundle getuserID = getIntent().getExtras(); 
    if (getuserID != null) { 
     userID = getuserID.getString("userID"); 
    } 

...

intent = new Intent().setClass(this, Games.class); 
    intent.putExtra("userID", userID); 
    spec = tabHost.newTabSpec("games").setIndicator("Games", 
         res.getDrawable(R.drawable.tab_games)) 
        .setContent(intent); 
    tabHost.addTab(spec); 
11

는 여전히 번들에 아무것도 넣지 않은 경우, 항상 nullmyIntent.putExtra("selection", select.ToString());

+3

예, String.valueOf를 추가합니다. 작동합니다! – Cheung

+0

편집 가능한 객체 인 EditText.getText()를 전달하면서 putExtra()가 두 개의 문자열을 사용하더라도 컴파일러는 편집 가능한 객체를 전달하는 것에 불평하지 않습니다. – glenneroo

+1

EditText.getText()에 의해 반환 된 Editable 객체에 대해 String.valueOf()를 사용해야하는 이유는 direclt를 Serializable에 매핑하므로 putExtra (String, Serializable)가 사용되므로 getSerializableExtra (String)은 불리다. – TheIT

2

반환되도록 myIntent.putExtra("selection", select);-.ToString() 추가! 활동에

Intent i = new Intent(...); 

i.putExtra("dummy", true); 

Bundle bundle= i.getExtras(); // (not null anymore) 
-1

:

Intent i = new Intent(...); 

i.putExtras(new Bundle()); 

Bundle bundle= i.getExtras(); // (not null anymore) 

아니면, 그냥 초기화 (내가 처음 솔루션을 선호)을 강제로 더미 값을 넣어 :

만들고 자신에 의해 번들을 설정할 수 있습니다 B 다음 기능에서 당신을 :

protected String getStringExtra(Bundle savedInstanceState, String id) { 
    String l; 
    l = (savedInstanceState == null) ? null : (String) savedInstanceState 
      .getSerializable(id); 
    if (l == null) { 
     Bundle extras = getIntent().getExtras(); 
     l = extras != null ? extras.getString(id) : null; 
    } 
    return l; 
} 

당신은 다음과 같은 방식으로 사용하고 있습니다. 띠 모양 B :

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    String MANEUVER_ID = getStringExtra(savedInstanceState, "selection");  
} 
4

아래 코드를 사용해보십시오. 귀하의 코드에 재정의를 추가 했으므로 트릭을 수행하게됩니다.

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

    Intent i = getIntent(); 

    if (i == null) 
     Log.d("***DEBUG****", "Intent was null"); 
    else 
     Log.d("**** DEBUG ***", "Intent OK"); 

    String MANEUVER_ID = i.getStringExtra("selection"); //Exception points to this line 
    Log.d("*** DEBUG", rec + " " + MANEUVER_ID); 
} 
관련 문제