崩溃的应用程序。 Android系统。 OnClick共享首选项

时间:2018-05-10 17:38:14

标签: android onclick sharedpreferences

应用程序在onclick上一直崩溃。调试中没有错误。在editor.apply()或开始下一个活动时崩溃。无法弄清楚原因。当它崩溃时,它会转到白色屏幕,然后重新加载主要活动。使用android studio和kindle。试过模拟器,它仍然没有工作。尝试重写和检查权限没有任何效果。应用程序的其余部分工作正常,我在其他活动中访问共享首选项。任何帮助都很好。谢谢。

package com.jakewalther.x;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;



public class newUser extends AppCompatActivity {
EditText ename, eemail, enumber,eage, epin;
Button button;
String malefem;
String name, email, number, pin, age;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_user);

    ename = findViewById(R.id.Name);
    eemail = findViewById(R.id.Email);
    enumber = findViewById(R.id.Phone);
    epin = findViewById(R.id.Pin);
    button = findViewById(R.id.create);


    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            CheckBox boxM = findViewById(R.id.Male);
            CheckBox boxF = findViewById(R.id.Female);

            SharedPreferences settings = getSharedPreferences("info", MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();

            editor.clear();

            if (boxM.isChecked()) {

                if (boxF.isChecked()) {
                    malefem = "Trans";
                } else {
                    malefem = "Male";
                }
            } else if (boxF.isChecked()) {
                malefem = "Female";
            }

            name = ename.getText().toString();
            pin = epin.getText().toString();
            email = eemail.getText().toString();
            number = enumber.getText().toString();
            age = eage.getText().toString();

            SharedPreferences.Editor sex = editor.putString("sex", malefem);
            SharedPreferences.Editor username = editor.putString("username", name);
            SharedPreferences.Editor password = editor.putString("pin", pin);
            SharedPreferences.Editor xmail = editor.putString("email", email);
            SharedPreferences.Editor phone = editor.putString("phone", number);

            SharedPreferences.Editor ager = editor.putString("age", age);
            editor.apply();

            Intent next = new Intent(getApplicationContext(), LoadXOrgasm.class);
            startActivity(next);
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

我认为问题在于你正在创建很多Editor对象的实例。

尝试这样做:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences("info",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor=settings.edit();
     editor.putString("sex", malefem);
     editor.putString("username", name);
     editor.putString("pin", pin);
     editor.putString("email", email);
     editor.putString("phone", number);
     editor.putString("age", age);
     editor.apply();