有关BottomSheetDialog可见性的问题

时间:2019-05-12 20:17:19

标签: java android

我的showUpdateDialog方法似乎有问题。当应用程序运行时,没有对话框出现。

检查XML文件中的名称是否重复/冲突。

未记录任何异常。

XML文件:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="6dp"
        tools:layout_editor_absoluteY="8dp">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/etUpdateUserName"
            android:hint="Your Name"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/etUpdateUserAddress"
            android:hint="Your Address"
            android:inputType="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_update"
            android:background="@drawable/custom_button_style"
            android:fontFamily="casual"
            android:text="Update Information"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            />

    </android.support.design.widget.TextInputLayout>'


</LinearLayout>

我的MainActivity,包括showUpdateDialog方法:

public class HomeActivity extends AppCompatActivity {

@BindView(R.id.bottom_navigation)
BottomNavigationView bottomNavigationView;

CollectionReference userRef;

BottomSheetDialog bottomSheetDialog;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ButterKnife.bind(HomeActivity.this);


    userRef = FirebaseFirestore.getInstance().collection("User");



    if(getIntent() !=null)
    {
        boolean isLogin = getIntent().getBooleanExtra(Common.IS_LOGIN, false);
        if(isLogin)
        {

            AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(final Account account) {
                    if(account !=null)
                    {
                        DocumentReference currentUser = 
userRef.document(account.getEmail().toString());
                        currentUser.get()
                                .addOnCompleteListener(new 
OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull 
Task<DocumentSnapshot> task) {
                                        if(task.isSuccessful())
                                        {
                                            DocumentSnapshot userSnapshot = 
task.getResult();
                                            if(!userSnapshot.exists())
                                            {

showUpdateDialog(account.getEmail().toString());
                                            }

                                        }
                                    }
                                });
                    }
                }




                @Override
                public void onError(AccountKitError accountKitError) {
              Toast.makeText(HomeActivity.this, 
""+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


    bottomNavigationView.setOnNavigationItemSelectedListener(new 
BottomNavigationView.OnNavigationItemSelectedListener() {
       Fragment fragment = null;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if(menuItem.getItemId() == R.id.nav_home)
                fragment = new HomeFragment();
            else if(menuItem.getItemId() == R.id.nav_shopping)
                fragment = new ShoppingFragment();

            return loadFragment(fragment);
        }
    });


    bottomNavigationView.setSelectedItemId(R.id.nav_home);

}

private boolean loadFragment(Fragment fragment) {
    if(fragment !=null)
    {

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, 
fragment)
        .commit();
        return true;
    }
    return false;
}

private void showUpdateDialog(final String email)
{
    bottomSheetDialog = new BottomSheetDialog(HomeActivity.this);
    bottomSheetDialog.setTitle("One last step...");
    bottomSheetDialog.setCanceledOnTouchOutside(false);
    bottomSheetDialog.setCancelable(false);
    View sheetView = 
getLayoutInflater().inflate(R.layout.layout_update_user_information, null);

    Button btn_update = (Button)sheetView.findViewById(R.id.btn_update);
    final TextInputEditText edit_name = 
(TextInputEditText)sheetView.findViewById(R.id.etUpdateUserName);
    final TextInputEditText edit_address = 
(TextInputEditText)sheetView.findViewById(R.id.etUpdateUserAddress);

    btn_update.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v)
        {

            User user = new User(edit_name.getText().toString(),
                    edit_address.getText().toString(),
                    email);
            userRef.document(email)
                    .set(user)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            bottomSheetDialog.dismiss();

                            Toast.makeText(HomeActivity.this, "Details 
updated", Toast.LENGTH_SHORT);
                        }
                    }) .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e)
                {

                    bottomSheetDialog.dismiss();
                    Toast.makeText(HomeActivity.this, ""+e.getMessage(), 
Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

    bottomSheetDialog.setContentView(sheetView);
    bottomSheetDialog.show();


}




}

我希望bottomSheetDialogBox能够显示出来,使layout_update_user_information膨胀起来。

1 个答案:

答案 0 :(得分:0)

可能是因为您没有使用适当的上下文显示对话框

方法的第一行

bottomSheetDialog = new BottomSheetDialog(this);

您的上下文引用在哪里?

它必须是您的活动或片段的上下文

尝试像这样更改方法参数:

private void showUpdateDialog(Context context, final String email)

并将正确的Context传递给您的方法,例如

showUpdateDialog(YourActivityClassName.this,"emailAddress");

showUpdateDialog(YourFragmentClassName.this,"emailAddress");

P.S: 确保您正在从UI线程调用方法:

runOnUiThread(new Runnable(){
 public void run() {
      showUpdateDialog(HomeActivity.this,email);
 }
});