Firebase应用程序更新用户

时间:2017-12-30 23:03:19

标签: android firebase android-fragments firebase-authentication

我创建了我的第一个firebase应用程序,到目前为止,从用户注册到身份验证的所有工作都正常。

我创建了一个名为 ManageAccountFragment 的片段,我将使用该片段为用户添加名称

ManageAccountFragment.java

package com.company.walt.fragments;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.company.walt.R;

import static com.android.volley.VolleyLog.TAG;

/**
 * A simple {@link Fragment} subclass.
 */
public class ManageAccountFragment extends Fragment {

    private EditText mName;
    private Button mUpdate;

    private ProgressBar mProgressBar;

    public ManageAccountFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_manage_account, container, false);

        mName = getView().findViewById(R.id.input_name);
        mUpdate = getView().findViewById(R.id.btn_update);

        mProgressBar = getView().findViewById(R.id.progressBar);

        mUpdate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view){

                if (!isEmpty (mName.getText().toString()))
                {

                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                    if (user != null)
                    {
                        UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                                .setDisplayName(mName.getText().toString())
                                //.setPhotoUri(Uri.parse("https://avatarfiles.alphacoders.com/862/86285.jpg"))
                                .build();

                        user.updateProfile(profileUpdate).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful())
                                {
                                    Log.d(TAG, "onComplete: User Profile updated");
                                }
                            }
                        });

                    }

                }

            }

        });

        return view;
    }

    private boolean isEmpty(String string){
        return string.equals("");
    }

    private void showDialog(){
        mProgressBar.setVisibility(View.VISIBLE);

    }

    private void hideDialog(){
        if(mProgressBar.getVisibility() == View.VISIBLE){
            mProgressBar.setVisibility(View.INVISIBLE);
        }
    }

    private void hideSoftKeyboard(){
        //this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

}

fragment_manage_account.xml

<android.support.v4.widget.SwipeRefreshLayout 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/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/app_background"
    android:clickable="true"
    android:focusable="true"
    tools:context="com.company.walt.fragments.ManageAccountFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/fra_home"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.CardView
                android:id="@+id/card_currency"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/ssi_16dp"
                android:layout_marginLeft="@dimen/ssi_16dp"
                android:layout_marginRight="@dimen/ssi_16dp"
                android:layout_marginStart="@dimen/ssi_16dp"
                android:layout_marginTop="@dimen/ssi_16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/ssi_180dp"
                    android:background="?attr/card_background"
                    android:gravity="center_vertical"
                    android:orientation="vertical"
                    android:paddingEnd="@dimen/ssi_16dp"
                    android:paddingStart="@dimen/ssi_16dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Update you information"
                        android:textColor="?attr/card_text"
                        android:textSize="@dimen/ssi_txt_16sp" />

                    <EditText
                        android:id="@+id/input_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/ssi_32dp"
                        android:hint="Name"
                        android:inputType="textEmailAddress"
                        android:textColor="@color/bone"
                        android:textColorHint="@color/gyro" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/card_options"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/card_currency"
                android:layout_marginEnd="@dimen/ssi_16dp"
                android:layout_marginLeft="@dimen/ssi_16dp"
                android:layout_marginRight="@dimen/ssi_16dp"
                android:layout_marginStart="@dimen/ssi_16dp"
                android:layout_marginTop="@dimen/ssi_16dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/ssi_180dp"
                    android:background="?attr/card_background"
                    android:gravity="center_vertical"
                    android:orientation="vertical"
                    android:paddingEnd="@dimen/ssi_16dp"
                    android:paddingStart="@dimen/ssi_16dp">

                    <android.support.v7.widget.AppCompatButton
                        android:id="@+id/btn_update"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/ssi_16dp"
                        android:text="Update"
                        android:textColor="@color/bone" />

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center">

                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="@dimen/ssi_144dp"
                    android:layout_height="@dimen/ssi_144dp"
                    android:layout_centerHorizontal="true"
                    android:visibility="invisible" />

            </RelativeLayout>

        </RelativeLayout>

    </ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

logcat的

12-30 17:58:39.557 21262-21262/com.company.walt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.company.walt, PID: 21262
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.company.walt.fragments.ManageAccountFragment.onCreateView(ManageAccountFragment.java:48)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

导航到这个片段导致崩溃,我不知道为什么会这样。

我很感激帮助。

1 个答案:

答案 0 :(得分:1)

请使用你的膨胀设计的你的视图变量来注册片段onCreateView()方法中的子视图:

View view = inflater.inflate(R.layout.fragment_manage_account, container, false);

        mName = view.findViewById(R.id.input_name);
        mUpdate =view.findViewById(R.id.btn_update);

        mProgressBar = view.findViewById(R.id.progressBar);