我是 Android 编程的新手,我有一个带有 imageButton 的 Activity ,当它被点击时,它会打开一个新的 Activity 以获取带有对话框主题的结果。用户输入一些数据并点击提交。然后我需要从 Intent 中获取数据以更新第一个 Activity 的一些图像。问题是当我尝试访问 Intent 中的字符串时,我不断收到 NullPointerException。
第一个 Activity 的点击方法:
public void openDetails(View view){
Intent intent = new Intent(this, FinalAnalysisDialog.class);
//add to backStack
startActivityForResult(intent, 1);
}
第二个 Activity (对话主题):
@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);
}
}
我尝试发送一个静态字符串“In process”来查看问题是否可能是从 spinner 中获取值,但这没有用。我搜索过类似的问题,但我似乎无法解决这个问题。我错过了什么?任何帮助将不胜感激..
这是我的日志:
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
最佳答案
你的问题是你正在从包中检索字符串而不是 Intent 本身,尝试使用:
String result = data.getStringExtra("result");
或者简单地取消注释行:
//intent.putExtras(bundle);
并像您最初尝试的那样检索字符串。
关于android - 尝试从 onActivityResult 中获取数据时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471502/