1

我是 Android 编程新手,我有一个带有 imageButton 的活动,当它被点击时,它会打开一个带有对话框主题的结果的新活动。用户输入一些数据并点击提交。然后我需要从更新第一个活动的一些图像的意图中获取这些数据。问题是当我尝试访问意图中的字符串时,我不断收到 NullPointerException。

第一个活动的 onclick 方法:

public void openDetails(View view){

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

    }

第二个活动(对话主题):

@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
4

1 回答 1

4

您的问题是您正在从包中检索字符串而不是意图本身,请尝试使用:

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

或简单地取消注释该行:

//intent.putExtras(bundle);

并按照您最初尝试的方式检索字符串。

于 2013-05-09T21:39:33.243 回答