Android “新建波士顿教程”按钮的共享首选项不适用于单击
我一直在寻找,但没有找到解决我问题的办法。 我是android新手,并遵循波士顿新教程。 在SharedReferences中,活动按钮在单击时不起作用。未显示任何内容无异常无任何内容 我的代码是:Android “新建波士顿教程”按钮的共享首选项不适用于单击,android,sharedpreferences,Android,Sharedpreferences,我一直在寻找,但没有找到解决我问题的办法。 我是android新手,并遵循波士顿新教程。 在SharedReferences中,活动按钮在单击时不起作用。未显示任何内容无异常无任何内容 我的代码是: package com.ss; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; imp
package com.ss;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SharedPrefs extends Activity implements OnClickListener {
TextView sharedData;
EditText showResults;
SharedPreferences somedata;
public static String filename = "MySharedString";
@Override
public void onClick(View v) {
try {
switch (v.getId()) {
case R.id.bSave:
String stringData = showResults.getText().toString();
Editor editor = somedata.edit(); // Editor from //
// sharedpreferecnes
editor.putString("ourString", stringData);
editor.commit();
break;
case R.id.bLoad:
if (somedata.contains("ourString")) {
// getting data
String get = somedata.getString("ourString", null);
showResults.setText(get);
}
break;
}
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.sharedpreferences);
setupVariables();
somedata = getSharedPreferences(filename, 0);
} catch (Exception e) {
}
}
public void setupVariables() {
try {
// TODO Auto-generated method stub
Button save = (Button) findViewById(R.id.bSave);
Button load = (Button) findViewById(R.id.bLoad);
sharedData = (TextView) findViewById(R.id.tvLoad);
showResults = (EditText) findViewById(R.id.tvShowresults);
save.setOnClickListener(this);
load.setOnClickListener(this);
} catch (Exception e) {
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etShowresults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/bLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load" />
<TextView
android:id="@+id/tvLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Your Data" />
</LinearLayout>
package com.ss;
导入android.app.Activity;
导入android.app.Dialog;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
公共类SharedPrefs扩展活动实现OnClickListener{
文本视图共享数据;
编辑文本显示结果;
共享数据引用;
公共静态字符串filename=“MySharedString”;
@凌驾
公共void onClick(视图v){
试一试{
开关(v.getId()){
案例R.id.b保存:
String stringData=showResults.getText().toString();
Editor Editor=somedata.edit();//编辑器来自//
//共享优先权
putString(“ourString”,stringData);
commit();
打破
案例R.id.bLoad:
if(somedata.contains(“ourString”)){
//获取数据
字符串get=somedata.getString(“ourString”,null);
showResults.setText(get);
}
打破
}
}捕获(例外e){
//TODO:处理异常
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
试一试{
super.onCreate(savedInstanceState);
setContentView(R.layout.SharedReferences);
设置变量();
somedata=getSharedReferences(文件名,0);
}捕获(例外e){
}
}
公共void setupVariables(){
试一试{
//TODO自动生成的方法存根
按钮保存=(按钮)findViewById(R.id.bSave);
按钮加载=(按钮)findViewById(R.id.bLoad);
sharedData=(TextView)findViewById(R.id.tvLoad);
showResults=(EditText)findViewById(R.id.tvShowresults);
save.setOnClickListener(这个);
load.setOnClickListener(这个);
}捕获(例外e){
}
}
}
像这样创建SharedReference
SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
Editor editor = preferences.edit();
//Now put values in editor
editor.putString(key_value_for_access ,string_value_to_store );
editor.commit();
//Now for accessing this into every activity you have to write the same thing:
SharedPrefrences preferences = getSharedPreferences(key_value,
Context.MODE_PRIVATE);
if(prefernces.contains(key_value_for_access)){
//getting data
String get = preferences.getString(key_value_for_access, null);
}
删除所有异常捕获,这对您来说除了隐藏正在发生的事情之外什么都没有。您创建对话框时从未显示任何对话框 另外,我希望,纯粹根据变量命名约定,
R.id.tvLoad
是一个TextView
,您试图将其分配给EditText
如果是这种情况,请在布局中使
R.id.tvLoad
成为EditText
。不起作用。当我按“保存”按钮和“加载”按钮时,它不起作用omedata=getSharedReferences(文件名、上下文.MODE\u PRIVATE)//在oncreate视图中使用这一行,并从load按钮单击listener进行替换。它应该在一个活动中声明一次。是的,你抓住了它。我已经改进了,但是问题仍然是一样的,我已经发布了我的代码。你仍然没有显示布局,请删除异常处理,这没有帮助。好的,再一次,删除“整个”异常处理,尝试,捕获,全部,它隐藏了一些问题。完成后,再次运行应用程序,然后发布stacktrace。因为它隐藏了一个问题!我现在才明白,您没有更新showResults=(EditText)findViewById(R.id.tvShowresults)中的id代码>最后一次,删除try/catch,运行应用程序,打印stacktrace。