1

我是Android編程的新手,我有一個具有imageButton的活動,當它被單擊時,它將爲具有對話框主題的結果打開一個新活動。用戶輸入一些數據並點擊提交。然後我需要獲取這些數據,以便更新第一個活動中的某些圖像。問題是,當我嘗試訪問意圖中的字符串時,我不斷收到NullPointerException。當嘗試在onActivityResult中獲取數據意圖時發生NullPointerException

1日活動的onclick方法:

public void openDetails(View view){ 

     Intent intent = new Intent(this, FinalAnalysisDialog.class); 
     //add to backStack 
     startActivityForResult(intent, 1); 

    } 

2日活動(對話的主題):

@Override 
    protected void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.final_analysis_activity); 

     WindowManager.LayoutParams params = getWindow().getAttributes(); 
     //params.x = -100; 
     params.height = 500; 
     params.width = 600; 
     //params.y = -50; 

     this.getWindow().setAttributes(params); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     //final?? 
     final Spinner dropDown = (Spinner) findViewById(R.id.faResultsDropDown); 
     dropDown.setSelection(2); 

     Button submit = (Button) findViewById(R.id.faSubmitButton); 
     submit.setOnClickListener(new OnClickListener() { 
      //based on the value of selected item from dropDown (spinner), update the 
      //progress status picture 
      @Override 
      public void onClick(View v) { 
       Bundle bundle = new Bundle(); 
       //dropDown.getSelectedItemPosition(); 
       String result = dropDown.getSelectedItem().toString(); 
       //String result = "In process"; 
       bundle.putString("result", result); 

       Intent intent = new Intent(); 
       //intent.putExtras(bundle); 
       intent.putExtra("result", result); 
       setResult(RESULT_OK, intent); 
     finish(); 
      } 
     }); 

然後我試圖讓弦退了出來:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode==RESULT_OK && requestCode==1){ 
      //ImageButton mFApic = (ImageButton)home.getView().findViewById(R.id.mInsStepImage);//andrology_home_fragment 

      ImageButton mFApic = (ImageButton)findViewById(R.id.mInsStepImage);//andrology_home_fragment 
      View mHomeFApic = (View)findViewById(R.id.maleFA);//male_details 
      //do something with result 
      //Intent dataResult = data.getData(); 
      //Bundle bundle =dataResult.getExtras(); 
      //String result = bundle.getString("result"); 

      Bundle extras = data.getExtras(); 
      String result = extras.getString("result"); 
      //String result = data.getStringExtra(); 
      if (result.equals("Not started")){ 
       mFApic.setBackgroundResource(R.drawable.orange_process); 
       mHomeFApic.setBackgroundResource(R.drawable.orange_process); 
      }else if (result.equals("In process")){ 
       mFApic.setBackgroundResource(R.drawable.orange_process); 
       mHomeFApic.setBackgroundResource(R.drawable.orange_process); 
      }else if (result.equals("Complete")){ 
       mFApic.setBackgroundResource(R.drawable.green_process); 
       mHomeFApic.setBackgroundResource(R.drawable.green_process); 
      }else if (result.equals("Problem")){ 
       mFApic.setBackgroundResource(R.drawable.red_process); 
       mHomeFApic.setBackgroundResource(R.drawable.red_process); 
      } 
     } 

我已經試圖發送一個靜態字符串「正在處理」,以查看問題是否可以從微調器中獲得價值,但這種方式無效。我搜索了類似的問題,但我似乎無法得到這個工作。我錯過了什麼?任何幫助將不勝感激..

這裏是我的logcat:

05-09 14:26:31.133: E/AndroidRuntime(4984): FATAL EXCEPTION: main 
05-09 14:26:31.133: E/AndroidRuntime(4984): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.seattleivf/com.example.seattleivf.TabActionBarHomeActivity}: java.lang.NullPointerException 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3179) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread.access$1100(ActivityThread.java:140) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.os.Looper.loop(Looper.java:137) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread.main(ActivityThread.java:4895) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at dalvik.system.NativeStart.main(Native Method) 
05-09 14:26:31.133: E/AndroidRuntime(4984): Caused by: java.lang.NullPointerException 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at com.example.seattleivf.TabActionBarHomeActivity.onActivityResult(TabActionBarHomeActivity.java:197) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.Activity.dispatchActivityResult(Activity.java:5347) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3175) 
05-09 14:26:31.133: E/AndroidRuntime(4984):  ... 11 more 
+0

也許試試intent.putExtra(「return-data」,true);在你開始活動之前 – JRowan 2013-05-09 21:38:17

+0

你試過我的解決方案嗎? – Daniel 2013-05-09 21:47:32

+0

哪一行是197你得到NullPointerException? stacktrace表明你正在返回的'Intent1'有額外的「,所以'data.getExtras()'不應該返回null。是否有可能您的某個'findViewById()'調用返回null,並且在使用該調用時您得到了NPE?你的代碼對我來說看起來很好。您可以添加調試日誌記錄來查看問題出在哪裏,也可以通過調試器逐步完成。 – 2013-05-09 23:00:39

回答

4

你[R的問題是,您檢索從包中的字符串,沒有意圖本身,請嘗試使用:

String result = data.getStringExtra("result"); 

或者乾脆取消對該行:

//intent.putExtras(bundle); 

和最初你特里結構檢索字符串d到。

+0

好吧,我能夠得到結果,但當我試圖訪問稍後在代碼中的視圖時崩潰了:'ImageButton mFApic =(ImageButton) findViewById(R.id.mInsStepImage); // andrology_home_fragment \t \t \t查看mHomeFApic =(查看)findViewById(R.id.maleFA);'如何從顯示它的活動中訪問片段中的視圖? – Sara 2013-05-13 23:06:45

相關問題