2015-06-17 50 views
-3

我在Intent上獲得NullPointerException。我從兩個不同的活動來到同一個活動。我的代碼是這樣的:nullpointer on intent android

if(getIntent().getStringExtra("action").equals("yes"){ // NULLPOINTER HERE 

//Some code 

} 
else if(getIntent().getStringExtra("action").equals("completed"){ 

// Some code 

} 

當我在putExtra發送「完成」,那麼它顯示的錯誤,當我送「是」,它工作正常。

+0

檢查'getIntent()。getStringExtra( 「行動」)!= null' –

+0

檢查,如果您使用額外的變量在這兩個地方的 「行動」。 –

+1

你錯過了右括號。 – moffeltje

回答

0

嘗試 -

if(getIntent().getStringExtra("action")!=null) 
{ 
if(getIntent().getStringExtra("action").equals("yes")){ 

//Some code 

} 
else if(getIntent().getStringExtra("action").equals("completed")){ 

// Some code 

} 
} 
0

嘗試這樣,

裏面添加這個您的onCreate

String yourvalue; 

@Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.new); 

    Bundle extras = getIntent().getExtras(); 
    yourvalue = extras.getString("STRINGVALUE"); 

    } 

//任何地方使用

if(yourvalue.equals("")) 
     { 
     //your value is empty 
     } 
     else 
     { 
     if(yourvalue.equals("yes")) 
     { 

     //Some code 

     } 
     else if(yourvalue.equals("completed")) 
     { 

     } 

     } 

檢查您的putextra數據被髮送到下一個意圖或不

Intent intent = new Intent(getActivity(),newAct.class);     
    intent.putExtra("STRINGVALUE", value); 
    startActivity(intent);