2013-05-07 35 views
0

在Android中,我想將一個Intent對象傳遞給一個函數,但始終得到一個NullPointerException。傳遞意圖函數拋出NullPointerException

我找不到任何消息來源指出這是不可能的。

基本上我有這樣的:

public Intent intent; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    intent = getIntent(); 
    int check = elsewhere.chk_intent(intent); //<=THROWS ERROR 

    } 

chk_intent()執行與所述意圖發送數據的一些功能,如果especieally一些額外的字段存在。

我試圖將getIntent()移入此函數,但這是不允許的。

任何幫助將不勝感激

+0

你需要初始化'Intent intent'實例,默認情況下它的'null' – Smit 2013-05-07 00:35:22

+0

什麼是'別處',你可以發佈它嗎?另外,發佈logcat – codeMagic 2013-05-07 00:44:00

+0

有更多的代碼比你發佈。例如,'別處'是一個對象,你做了一個「別處=新的別處()」嗎? – Dave 2013-05-07 00:48:24

回答

0

此活動是否由另一個活動調用?只有當先前的活動提供一個意圖當前活動的目的不爲空:

// previous activity 
Intent intent = new Intent(FirstActivity.class, SecondActivity.class); 
int value = 10; 
intent.putIntExtra("key", value); 
startActivity(intent); 

// use this if want to return data 
//startActivityForResult(intent); 

如果這個活動開始(主)活動或它不是由之前的任何活動調用,那麼getIntent()爲空,你需要初始化你自己的new意圖對象。

// current activity in onCreate() method 
if(getIntent() == null) { 
    Intent intent = new Intent(); 
    int check = elsewhere.chk_intent(intent); 
} 
相關問題