简体   繁体   English

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

[英]java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.widget.TextView

So right now my app (a QR code scanner) currently crashes when I try to run it. 因此,当我尝试运行它时,我的应用程序(QR码扫描仪)当前崩溃。 The resulting error message from debugging is this: 调试产生的错误消息是这样的:

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: E / AndroidRuntime:致命例外:主进程:com.example.a16007637.qrcodereader,PID:4032 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.a16007637.qrcodereader / com.example.a16007637.qrcodereader.MainActivity }:java.lang.ClassCastException:无法将android.support.constraint.ConstraintLayout强制转换为android.app.ActivityThread.handleLaunchActivity(ActivityThread.android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)上的android.widget.TextView。 java:2856)在android.app.ActivityThread.-wrap11(未知来源:0)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)在android.os.Handler.dispatchMessage(Handler.java:106 )的android.os.Looper.loop(Looper.java:164)的com.android.java.lang.reflect.Method.invoke(本机方法)的android.app.ActivityThread.main(ActivityThread.java:6494)的)。在com.android.internal.os.ZygoteInit.main(ZygoteInit.java :)上的internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438): 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 co 807)由以下原因引起:java.lang.ClassCastException:android.support.constraint.ConstraintLayout无法在com.example.a16007637.qrcodereader.MainActivity.onCreate(MainActivity.java:33)上转换为com.example.a16007637.qrcodereader.MainActivity.onCreate(MainActivity.java:33) android.app.Activity.performCreate(Activity.java:7000)的.Activity.performCreate(Activity.java:7009)android.app.ActivityThread.performLaunchActivity(.android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)上的ActivityThread.java:2731)android.app.ActivityThread $ H.handleMessage(ActivityThread.java)上android.app.ActivityThread.-wrap11(未知源:0) :1589),位于android.os.Handler.dispatchMessage(Handler.java:106),位于android.os.Looper.loop(Looper.java:164),位于android.app.ActivityThread.main(ActivityThread.java:6494),位于Java com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)处的.lang.reflect.Method.invoke(本机方法) m.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) m.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

The main thing that is catching my eye is this line here: 吸引我注意的主要内容是此行:

ConstraintLayout cannot be cast to android.widget.TextView ConstraintLayout无法转换为android.widget.TextView

I tried to research more into this but all the answers I found online did not work for me unfortunately. 我尝试对此进行更多的研究,但不幸的是,我在网上找到的所有答案都对我不起作用。

My MainActivity.java code is: 我的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();
    }
}

And my XML file is here: 我的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>

Any help to resolve this issue would be greatly appreciated. 解决该问题的任何帮助将不胜感激。

You write incorrect id for your TextView. 您为TextView输入了错误的ID。 The id that you wrote is pointing to the ConstraintLayout object on XML. 您编写的ID指向XML上的ConstraintLayout对象。

Instead of (Line 31) : 代替(第31行):

QR_output = findViewById(R.id.output);

Write this : 写这个:

QR_output = findViewById(R.id.QROutput);

You have used the id output is for your root layout. 您已将id output用于您的根布局。 ie ConstraintLayout and it will give you the reference of ConstraintLayout . ConstraintLayout ,它将为您提供ConstraintLayout的参考。 Obviously a reference of ConstraintLayout cannot be stored in a variable of type TextView , which, if done, throws an exception. 显然,对ConstraintLayout的引用不能存储在TextView类型的变量中,如果完成,该变量将引发异常。

Use the following code to reference your TextView 使用以下代码引用您的TextView

QR_output = findViewById(R.id.QROutput);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.ScrollView无法强制转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.ImageButton无法转换为android.widget.TextView - java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.TextView java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView - java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:android.widget.TextView - java.lang.ClassCastException: android.widget.TextView 我不断收到错误 java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView - i keep getting the error java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView java.lang.ClassCastException:androidx.constraintlayout.widget.ConstraintLayout 无法转换为 android.widget.LinearLayout - java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.LinearLayout ConstraintLayout 不能转换为 android.widget.TextView - ConstraintLayout cannot be cast to android.widget.TextView android.support.constraint.ConstraintLayout无法转换为android.support.v4.widget.DrawerLayout - android.support.constraint.ConstraintLayout cannot be cast to android.support.v4.widget.DrawerLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM