2015-09-17 50 views
0

我正在做一個頁面,3個按鈕「保存」,這將保存文本,「VIew」將顯示剛剛保存的文本,「乾淨」將清理保存的數據。按鈕保存和查看Android

當我運行下面的代碼,應用程序發生衝突並停止運行,但它沒有顯示任何錯誤 我應該改變什麼?

謝謝!

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(); 
    } 
} 
+1

檢查logcat的 – Aakash

+1

清理項目,清潔你的logcat,並在明顯的,如果你不是以前添加它添加活動。 –

+0

你應該在'manifest.xml'文件中檢查你的'MainActivity.java'(也是'package name'),並檢查你的按鈕和編輯文本是否在你的'activity_main.xml'中有相同的id –

回答

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; 
    } 
} 
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();}