2013-01-18 29 views
-1

我试图做一个简单的程序,它使用SharedPreferences保存并检索字符串。应用程序通常会加载,但如果我点击按钮,应用程序会下降我不知道什么是错的。 下面是代码:在SharedPreferences中使用字符串

Shared.java

package com.example.sharedpreferences; 
import android.content.Context; 
import android.content.SharedPreferences; 
public class Shared { 
    SharedPreferences prefe; 
    SharedPreferences.Editor editor; 
    Context mycontext; 
    public Shared(Context context){ 
     mycontext = context; 
     prefe = mycontext.getSharedPreferences("Preference", 0); 
     editor = prefe.edit(); 
    } 
    public void setpref(String name, String value){ 
     editor.putString(name, value); 
     editor.commit(); 
    } 
    public String getvalue(String name){ 
     return prefe.getString(name, "Nothing!");  
    } 
} 

MainActivity.java

package com.example.sharedpreferences; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.TextView; 
public class MainActivity extends Activity { 
    Shared preferences; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     preferences = new Shared(getApplicationContext()); 
    } 

    public void btn_send(View button){ 
     TextView name = (TextView)findViewById(R.id.name2); 
     TextView value = (TextView)findViewById(R.id.value2); 
     String names = (String) name.getText(); 
     String values = (String) value.getText(); 
     preferences.setpref(names, values); 
    } 
    public void btn_read(View button){ 
     TextView name = (TextView)findViewById(R.id.name2); 
     TextView value = (TextView)findViewById(R.id.value2); 
     String names = (String) name.getText(); 
     String values = preferences.getvalue(names); 
     value.setText(values); 
    } 
} 

谢谢!

+0

你能发布logcat的输出?我的猜测是有一个NullPointerException – Jared

+0

我还没有见过这种特殊的方法(在一个类中重新使用pref对象)。我想知道在编辑器上调用“commit()”后,编辑器不再可用。你可以尝试将这个逻辑移到你的活动中,每次都得到一个新的SharedPreferernces和Editor参考吗?这至少会验证或无效重新使用对象的方法。这有意义吗? – EJK

+0

logcat中的错误在这里[link](http://txtup.net/DCml)。 EJK,抱歉,但我不知道你的意思。 – Bullman

回答

0

看来,错误是由您的铸造字符串的字符序列。您可以改用String superString = fromTextEdit.getText().toString();

+0

谢谢!问题在于缺少.toString() – Bullman

0

使用

value.getText().toString() 

//优先级

public static class MyAppPref { 

    public static final String SHARED_PREFERENCE  = "shared_preference"; 
    public static final String SP_TEXT_VALUE   = "sp_text_value"; 
    public static final String SP_TEXT_VALUE_DEFAULT = "Nothing!"; 
    public static final int MODE_PRIVATE    = 0; 

    public static SharedPreferences getPref(Context ctx){ 
     return ctx.getSharedPreferences(SHARED_PREFERENCE , MODE_PRIVATE); 
    } 

    public static void saveTextValue(Context ctx, String value){ 
      getPref(ctx).edit().putString(SP_TEXT_VALUE, value).commit(); 
    } 

    public static String getStoredTextvalue(Context ctx){ 
      return getPref(ctx).getString(SP_TEXT_VALUE, SP_TEXT_VALUE_DEFAULT);  
    } 

} 

// Activity类

//android:onClick="btn_write_pref" 
public void btn_write_pref(View button){ 
    TextView value = (TextView)findViewById(R.id.textview_value); 
    String values = (String) value.getText(); 
    MyAppPref.saveTextValue(this, value.getText().toString()); 
} 
//android:onClick="btn_read_pref" 
public void btn_read_pref(View button){ 
    TextView value = (TextView)findViewById(R.id.textview_value); 
    value.setText(MyAppPref.getStoredTextvalue(this); 
} 
+0

谢谢!这非常有帮助。 – Bullman

0

我只是修改你的代码,它的样子与您的代码。

次活动,

public class Second extends Activity{ 
TextView text1, text2; 
Button send, read; 
Shared preferences; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    preferences = new Shared(getApplicationContext()); 

    text1 = (TextView)findViewById(R.id.text1); 
    text2 = (TextView)findViewById(R.id.text2); 
    send = (Button)findViewById(R.id.button1); 
    send.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String names = (String) text1.getText(); 
      String values = (String) text2.getText(); 
      preferences.setpref(names, values+names); 
     } 
    }); 
    read = (Button)findViewById(R.id.button2); 
    read.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String names = (String) text1.getText(); 
      String values = preferences.getvalue(names); 
      text2.setText(values); 
     } 
    }); 

} 

}

和你的爱好级别一样一样的,你上面提到的什么(shared.java)

+0

非常感谢。 – Bullman

相关问题