有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVAClassCastException:安卓。支持限制ConstraintLayout无法强制转换为安卓。小装置。文本框

所以现在我的应用程序(二维码扫描器)在我尝试运行时崩溃了。调试产生的错误消息如下:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.a16007637.qrcodereader, PID: 4032 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a16007637.qrcodereader/com.example.a16007637.qrcodereader.MainActivity}: java.lang.ClassCastException: 安卓.support.constraint.ConstraintLayout cannot be cast to 安卓.widget.TextView at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at 安卓.app.ActivityThread.-wrap11(Unknown Source:0) at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at 安卓.os.Handler.dispatchMessage(Handler.java:106) at 安卓.os.Looper.loop(Looper.java:164) at 安卓.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.ClassCastException: 安卓.support.constraint.ConstraintLayout cannot be cast to 安卓.widget.TextView at com.example.a16007637.qrcodereader.MainActivity.onCreate(MainActivity.java:33) at 安卓.app.Activity.performCreate(Activity.java:7009) at 安卓.app.Activity.performCreate(Activity.java:7000) at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  at 安卓.app.ActivityThread.-wrap11(Unknown Source:0)  at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  at 安卓.os.Handler.dispatchMessage(Handler.java:106)  at 安卓.os.Looper.loop(Looper.java:164)  at 安卓.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:807)

吸引我眼球的主要是这句话:

ConstraintLayout cannot be cast to 安卓.widget.TextView

我试图对此进行更多的研究,但不幸的是,我在网上找到的所有答案都不适用于我

我的主要活动。java代码是:

import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.widget.Button;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import 安卓.view.View;
import com.google.zxing.integration.安卓.IntentIntegrator;
import com.google.zxing.integration.安卓.IntentResult;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button Scan;
    private TextView QR_output;
    private IntentIntegrator ScanCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // Defines the Scan button
        Scan = findViewById(R.id.Scan);
        // defines the output for text
        QR_output = findViewById(R.id.output);

        // looks for the user clicking "Scan"
        Scan.setOnClickListener(this);

        ScanCode = new IntentIntegrator(this);
        // Means the scan button will actually do something
        Scan.setOnClickListener(this);
    }

    // will scan the qr code and reveal its secrets
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            // if an empty QR code gets scanned it returns a message to the user
            //if qr contains data
            if (result.getContents() == null) {
                Toast.makeText(this, "This QR code is empty.", Toast.LENGTH_LONG).show();
            } else try {
                //converting the data to json
                JSONObject obj = new JSONObject(result.getContents());
                //setting values to textviews
                QR_output.setText(obj.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
                //if control comes here
                //that means the encoded format not matches
                //in this case you can display whatever data is available on the qrcode
                //to a toast
                Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
    @Override
    public void onClick(View view) {
        //initiating the qr code scan
        ScanCode.initiateScan();
    }
}

我的XML文件在这里:

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/output"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        安卓:id="@+id/Scan"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="148dp"
        安卓:layout_marginTop="514dp"
        安卓:layout_marginEnd="148dp"
        安卓:layout_marginBottom="5dp"
        安卓:text="@string/Scan"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        安卓:id="@+id/QROutput"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="172dp"
        安卓:layout_marginTop="358dp"
        安卓:layout_marginEnd="166dp"
        安卓:layout_marginBottom="137dp"
        安卓:text="output"
        app:layout_constraintBottom_toTopOf="@+id/Scan"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</安卓.support.constraint.ConstraintLayout>

如能为解决此问题提供任何帮助,我们将不胜感激


共 (2) 个答案

  1. # 1 楼答案

    您为TextView写入了错误的id。您编写的id指向XML上的ConstraintLayout对象

    而不是(第31行):

    QR_output = findViewById(R.id.output);
    

    写下:

    QR_output = findViewById(R.id.QROutput);
    
  2. # 2 楼答案

    您已经将idoutput用于根布局。i、 e.ConstraintLayout,它将为您提供ConstraintLayout的参考。显然,ConstraintLayout的引用不能存储在类型为TextView的变量中,如果这样做,就会引发异常

    使用以下代码引用TextView

    QR_output = findViewById(R.id.QROutput);