当前位置: 首页 > 知识库问答 >
问题:

Java语言lang.ClassCastException:android。支持限制无法将ConstraintLayout转换为android。小部件。文本框

陶和歌
2023-03-14

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

E/AndroidRuntime:致命异常:主进程:com。实例a16007637.qrcodereader,PID:4032 java。lang.RuntimeException:无法启动activity ComponentInfo{com.example.a16007637.qrcodereader/com.example.a16007637.qrcodereader.MainActivity}:java。lang.ClassCastException:android。支持限制无法将ConstraintLayout转换为android。小部件。android上的文本视图。应用程序。ActivityThread。在android上执行LaunchActivity(ActivityThread.java:2778)。应用程序。ActivityThread。android上的handleLaunchActivity(ActivityThread.java:2856)。应用程序。ActivityThread-android上的wrap11(未知源:0)。应用程序。android上的ActivityThread$H.handleMessage(ActivityThread.java:1589)。操作系统。处理程序。android上的dispatchMessage(Handler.java:106)。操作系统。活套。android上的loop(Looper.java:164)。应用程序。ActivityThread。java上的main(ActivityThread.java:6494)。朗,反思一下。方法在com上调用(本机方法)。Android内部的操作系统。RuntimeInit$MethodandArgscaler。在com上运行(RuntimeInit.java:438)。Android内部的操作系统。合子岩。main(ZygoteInit.java:807)由:java引起。lang.ClassCastException:android。支持限制无法将ConstraintLayout转换为android。小部件。com上的文本视图。实例a16007637.qrcodereader。主要活动。android上的onCreate(MainActivity.java:33)。应用程序。活动在android上执行创建(Activity.java:7009)。应用程序。活动在android上执行创建(Activity.java:7000)。应用程序。仪器仪表。android上的callActivityOnCreate(Instrumentation.java:1214)。应用程序。ActivityThread。在android上执行LaunchActivity(ActivityThread.java:2731)。应用程序。ActivityThread。handleLaunchActivity(ActivityThread.java:2856)

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

ConstraintLayout不能强制转换为android.widget.TextView

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

我的主要活动。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>

如能帮助解决这一问题,将不胜感激。

共有2个答案

阴焱
2023-03-14

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

使用以下代码引用您的TextView

QR_output = findViewById(R.id.QROutput);

南宫星波
2023-03-14

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

代替(第31行):

QR_output = findViewById(R.id.output);

写这个:

QR_output = findViewById(R.id.QROutput);
 类似资料:
  • 我无法在listview模板上修复此问题:我的帖子标题中有错误,但我不会将imageview转换为textview。这是我的代码: java代码的错误在我的文件的第58行,我将TextView id分配给TextView mq: 下面是日志: 06-25 16:08:32.497:D/Debug(14642):Prendo Textview MQ 06-25 16:08:32.497:D/Andr

  • 我不知道我在这方面哪里出了问题,我正在尝试在recyclerView中列出食谱的成分,我只是无法让onBindViewHolder工作,不管我尝试了什么。下面是适配器代码以及xml配方。班本质上,我需要向recyclerView显示Recipe类的结果。 食谱班 适配器 完整logcat RecipeList模型公共类RecipeList{ 更新的错误 尝试在空对象引用上调用虚方法“空androi

  • 我得到以下例外在Android当点击一个按钮把我从一个活动到另一个(我是新的Android开发,所以它可能不是最聪明的问题): JAVAClassCastException:android。小装置。RelativeLayout无法转换为android。小装置。编辑文本 我已经尝试过清理项目好几次了,我尝试过Android工具中的修复项目属性选项,我检查过我的xml是否有错误,但我似乎无法解决这个问

  • 在发帖之前,我已经读过很多类似的问题,但都没有运气。 当我确定错误正在出现时,代码部分是: 与该部分相关的特定XML文件是: 出现此异常的原因是什么?我如何修复? 当我使用线性时,我不完全确定为什么异常与相对布局有关。 请注意,我是Android新手,所以这可能不是最聪明的问题!提前感谢您的帮助! 按要求activity_main.xml代码(据我所知,我自己没有创建也没有碰过这个文件):

  • 我试图在工具栏上添加一个阴影,下面是这个例子https://stackoverflow.com/a/26904102/4273056 活动中的我的工具栏 这是我添加工具栏的xml文件 我在logcat java中得到了这个。ClassCastException:android。小装置。LinearLayout无法转换为android。支持v7。小装置。工具栏 我怎样才能摆脱这个错误?

  • 我用英文发布了一个应用程序。我还没有多语言支持的计划,所以第一个版本只支持默认语言。我为第二个版本添加了广告(admob),本地化突然更改为默认56种语言。我如何将其更改回仅默认语言?或者就这样离开可以吗?