1

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();
            }

        } 
                }
user3747512
  • 203
  • 1
  • 6
  • 15

3 Answers3

2

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.

Zoran
  • 1,484
  • 1
  • 10
  • 13
  • I tried by placing et1 in the fragment, but it still doesn't help. I get a null point exception at et.setText(barcont) – user3747512 Jun 25 '14 at 16:54
  • Can you help me on how to include onActivityResult inside a fragment. Or is it possible to set the edittext field outside of it? – user3747512 Jun 25 '14 at 18:09
  • I edited answer. I hope it will help you. Remove onActivityResult from activity and add it to fragment. Not too many things changed in it (getActivity() instead of getApplicationContext()). Also, include et1 in fragment... – Zoran Jun 25 '14 at 19:30
  • Are you suggesting me to delete onActivityResult from MainActivity and place it in Fragment? But isn't the onActivityResult linked to button click in the MainActivity. The function is not being resolved when I place it in the fragment. – user3747512 Jun 26 '14 at 07:35
  • Are you sure your button is in activity because your et1 is in fragment? Is your button in activity layout or in fragment layout? – Zoran Jun 26 '14 at 08:08
  • My button is in the fragment layout, fragment_main – user3747512 Jun 26 '14 at 08:12
  • Then you must move your button and button click to fragment. Initialize it and add onClickListener in onCreateView just like for et1. Or if you use onClick parameter in xml do like in this solution: http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments – Zoran Jun 26 '14 at 08:46
  • the problem is the OnClickListenter is working fine and I add it in the fragment. But the OnActivityResult doesn't work in my fragment. I get a warnign saying "The method onActivityResult(int, int, Intent) from the type new View.OnClickListener(){} is never used locally" when i put my onActivityResult inside the OnClickListener or if I place it outside OnClickListener I get an error saying "void is an invalid type for the variable onActivityResult". – user3747512 Jun 26 '14 at 09:00
  • Don't put it in onClickListener. Add @Override above onActivityResult. Your et1 is in fragment, so the code must go to fragment. Maybe button is working in activity code but maybe because of that OnActivityResult won't be called (because it is in fragment) or it wouldn't work properly – Zoran Jun 26 '14 at 09:09
  • I edited the code,and as seen I get "void is an invalid type for the variable onActivityResult". The error stays the same. – user3747512 Jun 26 '14 at 09:27
  • It seems onActivityResult is inside onCreateView and it shouldn't be. It is separate method, so put it below onCreateView just like i did! – Zoran Jun 26 '14 at 09:40
  • Oh Sorry, my bad. I forgot to edit it here correctly.. It is outside OnCreateView in my program and the OnResultsActivity is now not firing at all. On button click I got to barcode scanner, but after scanning I don't get anything except that I return back to my app. – user3747512 Jun 26 '14 at 09:45
  • And where do you call barcode from? I don't see it... Is it in activity? – Zoran Jun 26 '14 at 09:56
  • barcode is called from this intent, which calls an external app based on zxing library. From this the data is passed via intents back again to the strings in the OnActivityResult. the problem now once I click the button I get the barcode app open, i do the scanning. Now, my controls goes into the onActivityResult, but it always only satisifes the else condition even though the scan was successful. – user3747512 Jun 26 '14 at 10:08
  • Take a look at solution in this link: http://stackoverflow.com/questions/20013213/zxing-onactivityresult-not-called-in-fragment-only-in-activity. It is same problem like you have – Zoran Jun 26 '14 at 10:17
  • But in case of the example in the coment, the OnActivityResult wasn't called. In mine I am able to go into OnActivityResult class. Do you have any idea on how to invoke it properly to receive back the data? – user3747512 Jun 26 '14 at 10:43
  • In example OnActivityResult in activity is called and from there new fragment is made and shown. When fragment is called probably is fired OnActivityResult because there is comment // handle scan result in it – Zoran Jun 26 '14 at 11:25
  • Thanks! so much, finally it worked with a round about. I am however now trying to call it in a fragment. – user3747512 Jun 26 '14 at 11:54
0

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.

bstar55
  • 3,542
  • 3
  • 20
  • 24
0

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!!

user3747512
  • 203
  • 1
  • 6
  • 15