【问题标题】:why do the alertdialog crashes my app everytime为什么警报对话框每次都会使我的应用程序崩溃
【发布时间】:2021-06-09 13:37:05
【问题描述】:

问题是警报对话框根本不起作用,每次都崩溃

我用 youtube 检查了每一行, 但没办法

代码如下:

public void showupdatedialogue(String id , String name){

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialog = inflater.inflate(R.layout.update,null);
        builder.setView(dialog);

        EditText naame = findViewById(R.id.name);
        EditText rollno = findViewById(R.id.roll);
        EditText cla = findViewById(R.id.clas);
        EditText date = findViewById(R.id.dob);
        EditText teacher = findViewById(R.id.teacher);
        Button up = findViewById(R.id.update);

        builder.setTitle("Updating" + " " + name + " " + "record");
        builder.show();

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = naame.getText().toString();
                String r = rollno.getText().toString();
                String c = cla.getText().toString();
                String d = date.getText().toString();
                String t = teacher.getText().toString();
                updatestudents(id,n,r,d,c,t);

                Toast.makeText(MainActivity.this,"record updated",Toast.LENGTH_LONG).show();

            }
        });


    }```

【问题讨论】:

    标签: android-studio firebase-realtime-database


    【解决方案1】:

    在 findViewById() 中你应该添加 dialog.findViewById()

    public void showupdatedialogue(String id , String name){
    
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View dialog = inflater.inflate(R.layout.update,null);
        builder.setView(dialog);
    
        EditText naame = dialog.findViewById(R.id.name);
        EditText rollno = dialog.findViewById(R.id.roll);
        EditText cla = dialog.findViewById(R.id.clas);
        EditText date = dialog.findViewById(R.id.dob);
        EditText teacher = dialog.findViewById(R.id.teacher);
        Button up = dialog.findViewById(R.id.update);
    
        builder.setTitle("Updating" + " " + name + " " + "record");
        builder.show();
    
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = naame.getText().toString();
                String r = rollno.getText().toString();
                String c = cla.getText().toString();
                String d = date.getText().toString();
                String t = teacher.getText().toString();
                updatestudents(id,n,r,d,c,t);
    
                Toast.makeText(MainActivity.this,"record updated",Toast.LENGTH_LONG).show();
    
            }
        });
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多