java - 有关 BottomSheetDialog 可见性的问题

标签 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。

最佳答案

这可能是因为您没有使用正确的上下文来显示对话框

在方法的第一行

bottomSheetDialog = new BottomSheetDialog(this);

你的Context的引用在哪里?

它必须是您的 Activity 或 Fragment 的上下文

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

private void showUpdateDialog(Context context, final String email)

并将正确的上下文传递给您的方法,例如

showUpdateDialog(YourActivityClassName.this,"emailAddress");

showUpdateDialog(YourFragmentClassName.this,"emailAddress");

附: 确保您从 UI 线程调用您的方法:

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

关于java - 有关 BottomSheetDialog 可见性的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56103119/

相关文章:

android - 无法通过 android studio 在 nexus 5/6.0.1 INSTALL_FAILED_INVALID_APK 上安装 apk

Android开发,VPNService.buider中的FileDescriptor不能读写

android - getChildCount() 返回错误的子级数

java - Android kotlin accountkit - 无法正常工作

java - 我如何使用 ldap 和 spring security 获取登录用户的电子邮件

java - Android:context.getDrawable() 的替代方案

java - hibernate : limit rows in a table

java - 将 JPanel 动态添加到 groupLayout

android - 如何正确设置 ZXingScannerView 的选项?

java - Spring 从 MQTT 出站 channel 适配器获取事件