"Binary XML file android.widget.ProgressBar cannot be cast to android.view.ViewGroup" 进度条中
"Binary XML file android.widget.ProgressBar cannot be cast to android.view.ViewGroup" in progress bar
我写了一个登录 xml 页面,我正在检查用户是否有资格通过 Firebase 数据库登录。现在输入用户名和密码后,我点击 "Sign-In" 按钮。在此之后加载主页需要几秒钟。所以我想显示一个进度条,它在那段时间旋转并在打开主页时停止。
我的唱入版面是:
<?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>
单击 "SignIn" 按钮时的代码片段是:
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;
}
}
我在执行此操作时遇到的错误是:
"Binary XML file android.widget.ProgressBar cannot be cast to 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>
您的布局文件只能有一个根目录。在这里你有两个。即 ProgessBar 和 ScrollView。尝试将它们封装在 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"
/>
并处理隐形/可见性。 :)
我写了一个登录 xml 页面,我正在检查用户是否有资格通过 Firebase 数据库登录。现在输入用户名和密码后,我点击 "Sign-In" 按钮。在此之后加载主页需要几秒钟。所以我想显示一个进度条,它在那段时间旋转并在打开主页时停止。
我的唱入版面是:
<?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>
单击 "SignIn" 按钮时的代码片段是:
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;
}
}
我在执行此操作时遇到的错误是: "Binary XML file android.widget.ProgressBar cannot be cast to 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>
您的布局文件只能有一个根目录。在这里你有两个。即 ProgessBar 和 ScrollView。尝试将它们封装在 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"
/>
并处理隐形/可见性。 :)