尝试在空对象引用上调用虚方法'void android.widget.edittext.settext(java.lang.charsequence)' [英] Attempt to invoke virtual method 'void android.widget.edittext.settext(java.lang.charsequence)' on a null object reference

查看:79
本文介绍了尝试在空对象引用上调用虚方法'void android.widget.edittext.settext(java.lang.charsequence)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在制作一个qr扫描仪应用程序,当我扫描代码时我想让它们进入编辑文本,但它一直给出错误:(((



这是我的代码:

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:




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" />





我尝试过:



i搜索解决方案1.5天:(,检查logcatt,显示:



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 :)

推荐答案

你不能引用视图项目只是名字。您需要为引用添加完整定位器: R.id.editText4 ,它对应于其完整路径名( id / editText4
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.


这篇关于尝试在空对象引用上调用虚方法'void android.widget.edittext.settext(java.lang.charsequence)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆