Click here to Skip to main content
15,943,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello ,
im making a qr scanner app , and when i scan the code i want them to go in a edittext , but it keeps giving errors :(((

this is my code :
 @Override
    public void handleResult(Result result) {
        //handle result
        Log.v("handleResult", result.getText());

        mScannerView.stopCamera();
        updateScannerData(1,result.getText());


//resume scanning
        mScannerView.resumeCameraPreview(this);

    }
    private void updateScannerData(int scanType, String scannedCode) {

        EditText editText;
        editText = findViewById(editText4);
        //EditText editText2  = (ditText2)findViewById(R.id.editText2);                beta lol
        editText.setText(scannedCode);
    }



xml:
<pre lang="xml"> <Button
        android:layout_width="218dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText4"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_red_light"
        android:onClick="onClick"
        android:text="@string/scan_qr"
        tools:text="scan qr code" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="197dp"
        android:ems="10"
        android:hint="@string/scan_order"
        android:inputType="textPersonName"
        android:visibility="visible"
        tools:backgroundTint="@android:color/holo_red_light" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="67dp"
        android:ems="10"
        android:hint="@string/scan_locatie"
        android:inputType="textPersonName"
        tools:backgroundTint="@android:color/holo_red_light" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="33dp"
        android:background="@android:color/holo_red_light"
        android:text="@string/button"
        tools:text="Versturen.." />

    <Button
        android:id="@+id/button2"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText2"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_red_light"
        android:onClick="onClick"
        android:text="@string/scan_qr"
        tools:text="scan qr code" />

    <Button
        android:id="@+id/button3"
        android:layout_width="40dp"
        android:layout_height="38dp"
        android:layout_alignStart="@+id/button4"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="10dp"
        android:background="@android:drawable/ic_delete" />

    <Button
        android:id="@+id/button4"
        android:layout_width="39dp"
        android:layout_height="37dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="136dp"
        android:background="@android:drawable/ic_delete" />


What I have tried:

i searched for a solution 1.5 day :( , examinate the logcatt which shows this :
<pre> java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference<br />
        at com.example.mvaartjes.mcmqr_reader.MainActivity.updateScannerData(MainActivity.java:100)<br />
        at com.example.mvaartjes.mcmqr_reader.MainActivity.handleResult(MainActivity.java:88)</pre>



any help is GREATLY APPRECIATED :)
Posted
Updated 26-Jul-18 22:43pm

1 solution

You cannot reference view items just by the name. You need to add the full locator for the reference: R.id.editText4, which corresponds to its full pathname (id/editText4) in the Resource set.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900