当前位置: 首页 > 知识库问答 >
问题:

我怎么能得到一个输入的文本输入布局作为共享的首选项,并打印他们在另一个活动?

桂杰
2023-03-14

我正在尝试使用共享首选项并将我的数据发送到另一个活动中。当我单击注册文本视图时,应用程序崩溃并收到错误消息

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference
        at com.example.irongym.RegistrationActivity.onCreate(RegistrationActivity.java:45)

在我使用EditText的其他情况下,我使用了类似的方法,而且很有效。我认为我尝试获取数据的方式是错误的,因为现在我使用的是TextInputLayout

这是我的注册活动。java(onCreate)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);

    TextInputLayout nameField = findViewById(R.id.TextInputEditText);
    TextInputLayout passwordField = findViewById(R.id.PasswordInputEditText);
    TextInputLayout phoneField = findViewById(R.id.PhoneInputEditText);

    String na = file.getString("name", "");
    String pas = file.getString("password2", "");
    String ph = file.getString("phone", "");

    nameField.getEditText().setText(na); // This is line 45 from the error i get
    passwordField.getEditText().setText(pas);
    phoneField.getEditText().setText(ph);

这是我的注册活动.java按钮点击

public void confirmInput(View v) {
    SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);
    SharedPreferences.Editor editor = file.edit();

    TextInputLayout nameField = findViewById(R.id.TextInputEditText);
    TextInputLayout passwordField = findViewById(R.id.PasswordInputEditText);
    TextInputLayout phoneField = findViewById(R.id.PhoneInputEditText);

    String name = nameField.getEditText().getText().toString();
    String password2 = passwordField.getEditText().getText().toString();
    String phone = phoneField.getEditText().getText().toString();

    editor.putString("name", name);
    editor.putString("password2", password2);
    editor.putString("phone", phone);

    editor.commit();
    
    Intent in = new Intent(this, LoginActivity.class);
    startActivity(in);
}

这是我的ProfileActivity.java,数据将在这里显示

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityProfileBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.toolbar);

    SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);

    String we = file.getString("weight", "no-weight");
    String he = file.getString("height", "no-height");
    String na = file.getString("name", "no-name");
    String ph = file.getString("phone", "no-phone");
    String ps = file.getString("password2", "no-password");

    TextView tvWe = findViewById(R.id.tvWeight);
    TextView tvHe = findViewById(R.id.tvHeight);
    TextView tvNa = findViewById(R.id.tvName);
    TextView tvPh = findViewById(R.id.tvPhone);
    TextView tvPs = findViewById(R.id.tvPassword);

    tvWe.setText(we);
    tvHe.setText(he);
    tvNa.setText(na);
    tvPh.setText(ph);
    tvPs.setText(ps);

    binding.fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

共有1个答案

曹高轩
2023-03-14

您忘记在RegistrationActivity中调用setContentView

 类似资料:
  • 问题内容: 我以这种方式使用了两个程序: c_program使用以下命令打印内容,而python_program.py使用以下命令读取内容 我想使python_program.py在打印时立即处理c_program的输出,以便它可以打印自己的当前输出。不幸的是python_program.py仅在c_program结束后才获得其输入。 我该如何解决? 问题答案: 只需将stdout设置为在C程序的

  • 问题内容: 我在购物车上工作,人们需要在同一页面上填写2个类似的表格。第一个表格是帐单地址,第二个表格是收货地址。两种形式都包含相似的输入元素,例如: a)帐单邮寄地址:姓名,地址行1,地址行2,国家/地区,电话等。 b)送货地址:姓名,地址行1,地址行2,国家/地区,电话等。 有一个复选框,上面写着“检查账单地址和送货地址是否相同”。因此, 即使仅选中此复选框, 我也需要将数据从帐单地址复制到送

  • 我目前正在用Java编写一个程序,其中我需要从用户那里获得输入,这是一段文本。然而,我需要用户能够在一次输入文本的多个段落。当程序提示输入时,只需粘贴整段文本就可以实现这一点。我对输入使用扫描器,当我将多个段落粘贴到输入中时,无论何时打印存储文本的变量,都不会抛出错误,只输出第一节(在第一个换行符之前)。如何使用多个换行符存储整段文本,而不提示用户为每个文本块输入单独的输入? 我已经有了一些代码,

  • 我在编程一个订单提交页面时遇到了一个相当大的问题,该页面的目的是提交一个订单的争议--提供两个字段被填写,但只有当一个字段少于另一个字段时。 基本上,一个是下拉,另一个是争端框,查询如下: 如果DispotestExtBox=“”而下拉框=“请选择...” 一切正常-提交按钮已启用 如果DisportestExtBox!=“”而下拉框=“请选择...” 错误(反之亦然,因此如果填充了Dispone