我正在制作这个需要用户名才能运行的程序。在设置中,意图运行良好,直到我添加了共享首选项代码。我可能在错误的地方添加了它。否则,意图绝对可以正常工作。共享首选项也不保存字符串。它继续崩溃。有任何想法吗?
package com.XXX.XXX;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Settings extends Activity{
EditText nameXML;
SharedPreferences filename;
public String name;
String filenamename = "nameFile";
Button save = (Button) findViewById(R.id.button1);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
nameXML = (EditText) findViewById(R.id.editText1);
filename = getSharedPreferences(filenamename,0);
String username = filename.getString("nameString", "Master Traders");
nameXML.setHint(username);
name = nameXML.getText().toString();
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Editor nameEditor = filename.edit();
nameEditor.putString("nameString", name);
nameEditor.commit();
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}