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

相关问题