簡體   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

因此,當我嘗試運行它時,我的應用程序(QR碼掃描儀)當前崩潰。 調試產生的錯誤消息是這樣的:

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)由以下原因引起: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)

吸引我注意的主要內容是此行:

ConstraintLayout無法轉換為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);

您已將id output用於您的根布局。 ConstraintLayout ,它將為您提供ConstraintLayout的參考。 顯然,對ConstraintLayout的引用不能存儲在TextView類型的變量中,如果完成,該變量將引發異常。

使用以下代碼引用您的TextView

QR_output = findViewById(R.id.QROutput);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM