java.lang.ClassCastException : android. support.constraint.ConstraintLayout 无法转换为 android.widget.TextView

标签 java android android-studio

现在,当我尝试运行我的应用程序(二维码扫描仪)时,它会崩溃。调试产生的错误消息是这样的:

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: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView at com.example.a16007637.qrcodereader.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:7009) at android.app.Activity.performCreate(Activity.java:7000) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

吸引我眼球的主要内容是这里的这一行:

ConstraintLayout cannot be cast to android.widget.TextView

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

我的MainActivity.java代码是:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.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"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/output"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/Scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="148dp"
        android:layout_marginTop="514dp"
        android:layout_marginEnd="148dp"
        android:layout_marginBottom="5dp"
        android: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
        android:id="@+id/QROutput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="172dp"
        android:layout_marginTop="358dp"
        android:layout_marginEnd="166dp"
        android:layout_marginBottom="137dp"
        android:text="output"
        app:layout_constraintBottom_toTopOf="@+id/Scan"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

任何解决此问题的帮助将不胜感激。

最佳答案

您为 TextView 输入的 ID 不正确。您编写的 id 指向 XML 上的 ConstraintLayout 对象。

代替(第 31 行):

QR_output = findViewById(R.id.output);

写下:

QR_output = findViewById(R.id.QROutput);

关于java.lang.ClassCastException : android. support.constraint.ConstraintLayout 无法转换为 android.widget.TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664495/

相关文章:

android-studio - Android Studio-Gradle组装任务

android - 调试器在 Android Studio 中不再活跃

java - 如何在分布式缓存产品中传输在多个分布式 map 上运行的事务

java - CodeChef - 厨师和幸运数字 : Why is no output being printed?

java - "restricted library"在 Java 中的真正含义是什么?

当 Activity 在后台时,Android 服务无法连接到服务器

android - 使用 ObjectAnimator 为 weightSum 属性设置动画

java - 计算JPA中2个日期之间的天数

android sqlite orderby datetime 总是获取第一行

android - 如何在 Android Studio 中调整 AVD 模拟器窗口的大小?