简体   繁体   中英

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent

I am creating an app which reads from barcode for the initial task. Below is the code block and the error generated

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {com.example.knr/com.example.knr.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at com.example.knr.MainActivity.onActivityResult(MainActivity.java:84)
    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)

Everything works fine, when I click the button I open the barcode scanner, but once I finish scanning I am getting this error. I tried to initiate a else so that I don't get a null exception, but still it stays the same.

Code Block for Main Fragment

    public class MainFragment extends Fragment {

                 public View onCreateView (LayoutInflater inflater, ViewGroup container,
                                             Bundle savedInstanceState) {
                        // Inflate the layout for this fragment
                        View view = inflater.inflate(R.layout.fragment_main, container, false);
                        Button button = (Button) view.findViewById(R.id.button);


        Button scan1 = (Button) view.findViewById(R.id.Scan);
                            final EditText et = (EditText) view.findViewById(R.id.editText); // also serves //toinput code scanned from barcode scanner

        scan1.setOnClickListener(new View.OnClickListener() {

                  public void onClick(View v) {
                   // Intent intent = new Intent("com.google.zxing.client.android.SCAN");
             //   getActivity().startActivityForResult(intent, 0);

                IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
                scanIntegrator.initiateScan();
              }
          });

                  button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                 // do some stuff for another button click and check for conditions

            }
                });
                return view
                }

@Override
              public void onActivityResult (int requestCode, int resultCode, Intent intent) {
            //retrieve scan result
            //super.onActivityResult(requestCode, resultCode, intent);
            IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
            if (scanningResult != null) {
                //we have a result

                String scanContent = scanningResult.getContents();
                //contentTxt.setText("CONTENT: " + scanContent);
                Toast toast = Toast.makeText(getActivity(), 
                        scanContent, Toast.LENGTH_SHORT);
                    toast.show();
                //et1.setText(scanContent);
                }
            else{
                Toast toast = Toast.makeText(getActivity(), 
                    "No scan data received!", Toast.LENGTH_SHORT);
                toast.show();
            }

        } 
                }

You are using fragment and you use EditText in onCreate instead in onCreateView of fragment. Later in onActivityResult you call et1.setText(barcont); and there you get null.

Try:

public static class MainFragment extends Fragment {

    EditText et1;

    public MainFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_display_grup,
            container, false);

        et1 = (EditText) rootView.findViewById(R.id.editText);
        return rootView;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {

                String contents = intent.getStringExtra("SCAN_RESULT");
                barcont=contents;
                Log.d(TAG,barcont);
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                Log.d(TAG,format);
                barfor=format;
            }
            else{
                Toast toast = Toast.makeText(getActivity(), 
                    "No data received!", Toast.LENGTH_SHORT);
                toast.show();}
        }

        et1.setText(barcont);
    }
}

Also, et1 should be global variable in Fragment, not activity and you should implement onActivityResult in fragment.

You're not correctly initializing your edit text:

final EditText et1 = (EditText) findViewById(R.id.editText);

Should be:

et1 = (EditText) findViewById(R.id.editText);

Since et1 is a member variable.

Solved,

the alternate or roundabout route to invoke the activity to write to EditText outside a MainFragment is to declare a variable in the MainActivity and assign the variable to the EditText in the Fragment by passing only the value to it.

example say the string or variable you need is

String X1= "output of barcode scanner"

then

import com.example.yourpackaganame.yourclassname 

into your Fragment class and invoke it by

et.setText(yourclassname.X1)

worked perfectly fine for me!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM