用于保存和查看Android的按钮

时间:2015-09-17 14:53:57

标签: android

我正在用3个按钮做一个页面"保存"这将保存文本," VIew"将显示刚保存的文字,"清洁"将清理保存的数据。

当我运行以下代码时,应用程序发生冲突并停止运行,但它没有显示任何错误 我应该改变什么?

谢谢!

public class MainActivity extends Activity implements View.OnClickListener {

EditText editTextName;
final String PREFS_NAME = "AOP_PREFS";
public static final String PREFS_KEY = "AOP_PREFS_String";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editTextName = (EditText)findViewById(R.id.editText);
    Button saveButton = (Button)findViewById(R.id.save);
    Button viewButton = (Button)findViewById(R.id.show);
    Button cleanButton = (Button)findViewById(R.id.clear);
    saveButton.setOnClickListener(this);
    viewButton.setOnClickListener(this);
    cleanButton.setOnClickListener(this);

}

@Override
public void onClick(View v) {

    if (v.getId() == R.id.save) {
        String name = editTextName.getText().toString();
        SharedPreferences.Editor editor;
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //Step 1
        editor = settings.edit(); // Step 2

        editor.putString(PREFS_KEY, name); // Step 3
        editor.commit(); // Step 4
    }

    if (v.getId() == R.id.show) {
        SharedPreferences settings;
        String name;
        SharedPreferences setting = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
        name = setting.getString(PREFS_KEY, null); //2
        TextView textview1 = (TextView) findViewById(R.id.textView);
        textview1.setText(name);

    }

    if (v.getId() == R.id.clear) {
        SharedPreferences settings;
        SharedPreferences.Editor editor;

        SharedPreferences set = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = set.edit();

        editor.clear();
        editor.commit();
    }
}

2 个答案:

答案 0 :(得分:0)

public class MainActivity extends Activity implements View.OnClickListener {

    EditText editTextName;
    final String PREFS_NAME = "AOP_PREFS";
    public static final String PREFS_KEY = "AOP_PREFS_String";
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextName = (EditText)findViewById(R.id.editText);
        Button saveButton = (Button)findViewById(R.id.save);
        Button viewButton = (Button)findViewById(R.id.show);
        Button cleanButton = (Button)findViewById(R.id.clear);
        saveButton.setOnClickListener(this);
        viewButton.setOnClickListener(this);
        cleanButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    SharedPreferences.Editor editor;
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //Step 1
    String name;
    switch( v.getId() ){
        case R.id.save:
            String editTextName = editTextName.getText().toString();
            editor = settings.edit(); // Step 2
            editor.putString(PREFS_KEY, editTextName ); // Step 3
            editor.commit(); // Step 4
            break;
        case R.id.show:
            String name = setting.getString(PREFS_KEY, null); //2
            TextView textview1 = (TextView) findViewById(R.id.textView);
            textview1.setText(name);
            break;
        case R.id.clear:
            editor = settings.edit();
            editor.setString( PREFS_KEY, "" );
            editor.commit();
            break;
    }
}

答案 1 :(得分:0)

Try putting a try catch on show button listener code, i.e.

 if (v.getId() == R.id.show) {
try{
        SharedPreferences settings;
        String name;
        SharedPreferences setting = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
        name = setting.getString(PREFS_KEY, null); //2
        TextView textview1 = (TextView) findViewById(R.id.textView);
        textview1.setText(name);
}catch (NullPointerException e)
{e.printStackTrace();}