java - 单击注册按钮时应用程序不断崩溃并且在设计模式下不显示预览?

标签 java android xml crash android-runtime

我在试图解决这个问题时遇到了严重的困难。我的应用程序中有一个登陆页面,当我单击注册按钮时,应用程序将崩溃,并且我会收到此错误;

             --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: ie.app.barbershop, PID: 3181
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.app.barbershop/ie.app.barbershop.Landing}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.access$800(ActivityThread.java:151)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:807)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at ie.app.barbershop.Landing.onCreate(Landing.java:23)
                      at android.app.Activity.performCreate(Activity.java:5990)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
I/Process: Sending signal. PID: 3181 SIG: 9
Application terminated.

这是我的 Landing.java 代码,我知道它与我的 setContent 有关,但我无法弄清楚

package ie.app.barbershop;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.view.ViewPager;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.ScrollView;

import android.widget.TextView;

import java.util.List;


public class Landing extends AppCompatActivity{

    public Button buttonProducts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_landing);

        countRecords();

        buttonProducts = findViewById(R.id.buttonProducts);

        Button buttonCreateAppointment = findViewById(R.id.buttonCreateAppointment);
        buttonCreateAppointment.setOnClickListener(new OnClickListenerCreateAppointment());

        buttonProducts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Landing.this, Products.class));
            }
        });


    }

    public void readRecords() {

        LinearLayout linearLayoutRecords = findViewById(R.id.linearLayoutRecords);
        linearLayoutRecords.removeAllViews();

        List<ObjectAppointment> appointments = new TableControllerAppointments(this).read();

        if (appointments.size() > 0) {

            for (ObjectAppointment obj : appointments) {


                String fullName = obj.fullName;
                int contactNumber = obj.contactNumber;

                String textViewContents = fullName + " - " + contactNumber;

                TextView textViewAppointmentItem= new TextView(this);
                textViewAppointmentItem.setPadding(0, 10, 0, 10);
                textViewAppointmentItem.setText(textViewContents);
                textViewAppointmentItem.setTag(Integer.toString(contactNumber));

                linearLayoutRecords.addView(textViewAppointmentItem);
            }

        }

        else {

            TextView locationItem = new TextView(this);
            locationItem.setPadding(8, 8, 8, 8);
            locationItem.setText("No records yet.");

            linearLayoutRecords.addView(locationItem);
        }

    }

    public void countRecords(){

        int recordCount = new TableControllerAppointments(this).count();

        TextView textViewRecordCount = findViewById(R.id.textViewRecordCount);
        textViewRecordCount.setText(recordCount + " records found.");

    }
}

此外,当我尝试在设计 View 中查看我的应用程序时,我收到此错误;

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:864)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:866)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:837)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:325)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:384)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:193)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:547)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$3(RenderTask.java:681)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这是我的 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/backgroundbarbershop">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="28dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/galleryImage"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_camera_alt_black_36dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="149dp"
        android:layout_height="144dp"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/gallery"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_shopping_cart_black_36dp" />

    <ImageView
        android:id="@+id/imageView11"
        android:layout_width="149dp"
        android:layout_height="144dp"
        android:layout_marginBottom="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:contentDescription="@string/appointmentPicture"
        app:layout_constraintBottom_toTopOf="@+id/buttonCreateAppointment"
        app:layout_constraintEnd_toStartOf="@+id/imageView12"
        app:layout_constraintHorizontal_bias="0.571"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/book" />

    <Button
        android:id="@+id/buttonCreateAppointment"
        android:layout_width="146dp"
        android:layout_height="54dp"
        android:layout_marginBottom="76dp"
        android:layout_marginStart="28dp"
        android:text="@string/buttonBookAppointment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/buttonProducts"
        android:layout_width="146dp"
        android:layout_height="38dp"
        android:layout_marginEnd="36dp"
        android:layout_marginTop="8dp"
        android:text="@string/buttonProducts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <Button
        android:id="@+id/buttonGallery"
        android:layout_width="145dp"
        android:layout_height="37dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/buttonGallery"
        app:layout_constraintBottom_toTopOf="@+id/imageView11"
        app:layout_constraintEnd_toStartOf="@+id/buttonProducts"
        app:layout_constraintHorizontal_bias="0.487"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button4"
        android:layout_width="140dp"
        android:layout_height="55dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/contactUsButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.314"
        app:layout_constraintStart_toEndOf="@+id/buttonCreateAppointment"
        app:layout_constraintTop_toBottomOf="@+id/imageView12"
        app:layout_constraintVertical_bias="0.157" />

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="wrap_content"
        android:layout_height="144dp"
        android:layout_marginEnd="40dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/contact"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonProducts"
        app:srcCompat="@drawable/ic_contact_phone_black_36dp" />


    <TextView
        android:id="@+id/textViewRecordCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:padding="1dp"
        android:text="@string/textViewRecordCount"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.182"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/buttonCreateAppointment"
        app:layout_constraintVertical_bias="0.146" >

        <LinearLayout
            android:id="@+id/linearLayoutRecords"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>

</TextView>
</android.support.constraint.ConstraintLayout>

最佳答案

您的 TextView 中有一个 LinearLayout:

<TextView
    android:id="@+id/textViewRecordCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:padding="1dp"
    android:text="@string/textViewRecordCount"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.182"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonCreateAppointment"
    app:layout_constraintVertical_bias="0.146" >

    <LinearLayout
        android:id="@+id/linearLayoutRecords"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>

</TextView>

如果您刚刚删除了线性布局应该可以正常工作。 TextView 不是 View Group,因此您不能在其中包含任何内容。这就是为什么你会得到:

AppCompatTextView cannot be cast to android.view.ViewGroup

关于java - 单击注册按钮时应用程序不断崩溃并且在设计模式下不显示预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49898841/

相关文章:

Java - 从字符串类型转换为泛型类型

java - Apache Ignite IGFS 不使用非堆空间

android - 我可以知道从 Playstore 安装了我的应用程序的用户的电子邮件 ID

c# - 为什么我会收到错误消息 : doubled attribute?

xml - 如何在 PowerShell 中获取单个 XML 元素

xml - 我应该如何使用 XML::Simple 处理 undefined reference ?

java - Interleave - 在 Java 中不能正常工作

java - 在不依赖集合的情况下从对象列表中删除重复项

android - 如何使用谷歌地图 API 而不是位置管理器获取用户当前位置坐标

android - 未调用 Crashlytics/Fabric initializationCallback