进度条中的"Binary XML file android.widget.ProgressBar cannot be cast to android.view.ViewGroup"



我写了一个登录XML页面,我正在检查用户通过Firebase数据库登录的资格。现在,输入用户名和密码后,我单击"登录"按钮。此后加载主页需要几秒钟。因此,我想显示一个在此期间旋转并在打开主页时停止的进度栏。

我唱歌的布局页面是:

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/loadingSignIn"
    android:indeterminate="true"
    android:visibility="invisible"
    >

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.hsports.bandpop.MainActivity"
        android:orientation="vertical"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:text="WEDDING PLANNER"
            android:gravity="center"
            android:textSize="50dp"

            />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:paddingTop="100dp"
            >
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Layout_username"
                android:hint="Username"
                >

                <AutoCompleteTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/username"
                    android:gravity="center"
                    />

            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Layout_password"
                android:hint="Password"
                >
                <AutoCompleteTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/password"
                    android:gravity="center"
                    android:inputType="textPassword"
                    />
            </android.support.design.widget.TextInputLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingTop="50dp"
                >
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="SIGN IN"
                    android:onClick="checkSignIn"
                    android:gravity="center"
                    android:id="@+id/SignInButton"
                    />
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="REGISTER"
                    android:onClick="registerForm"
                    android:gravity="center"
                    />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>
</ProgressBar>

单击"签名"按钮时,代码作为片段是:

package com.example.hsports.weddingplanner.Fragments;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.hsports.weddingplanner.Activities.FrontPage;
import com.example.hsports.weddingplanner.Activities.WeddingPlanningUsers;
import com.example.hsports.weddingplanner.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
/**
 * Created by I324671 on 11/28/2016.
 */
public class SignIn extends Fragment {

    SharedPreferences sharedPreferences;
    String MyPreferences="loginUserInfo";
    ProgressBar afterSignIn;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
        final View view=inflater.inflate(R.layout.activity_login_page, container, false);
        afterSignIn=(ProgressBar)view.findViewById(R.id.loadingSignIn);

        Button checkSignIn=(Button)view.findViewById(R.id.SignInButton);
        checkSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                afterSignIn.setVisibility(view.VISIBLE);
                final String username = ((TextView) (view.findViewById(R.id.username))).getText().toString();
                final String password = ((TextView) (view.findViewById(R.id.password))).getText().toString();
                FirebaseDatabase database = FirebaseDatabase.getInstance();
                DatabaseReference reference = database.getReference("Users");

                reference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.hasChildren()) {
                            for (DataSnapshot child : dataSnapshot.getChildren()) {
                                String usernameFromDB=child.getKey().toString();
                                String passwordFromDB=child.getValue().toString();
                                if(username.equals(usernameFromDB)&& password.equals(passwordFromDB))
                                {
                                    sharedPreferences=getContext().getSharedPreferences(MyPreferences, Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor=sharedPreferences.edit();
                                    editor.putString("Username",username);
                                    editor.putString("Password", password);
                                    editor.commit();

                                    afterSignIn.setVisibility(view.GONE);
                                    Intent transferToHome=new Intent(getContext(), FrontPage.class);
                                    startActivity(transferToHome);
                                    break;
                                }
                               }
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Log.d("The read failed", databaseError.getDetails());
                    }
                });
            }
        });
        return view;
    }
}

我执行此操作的错误是:"二进制XML文件android.widget.progressbar不能施放到android.view.viewgroup"。我该如何解决。

问题是你必须关闭XML内部的顶部标签:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

<ProgressBar
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/loadingSignIn"
    android:indeterminate="true"
    android:visibility="invisible"
    />

和:使用

   </LinearLayout>
</ScrollView>
   </LinearLayout>

您只能有一个布局文件的一个根。在这里你有两个。即前进和滚动浏览。尝试将它们封装在Framelayout中,一切都会好起来的。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout   xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="match_parent"
android:layout_width="match_parent">
<ProgressBar

android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
>

<ScrollView
    ...
  </ScrollView>
 </FrameLayout>

编辑 ->

为了做出进度栏中心,更改

<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="@+id/loadingSignIn"
android:indeterminate="true"
android:visibility="invisible"
/>

并处理隐形/可见性。:)

相关内容

最新更新