新波士顿教程按钮的共享首选项无法点击

shared preferences from new boston tutorial button not working on click

我搜索过 SO 但没有找到解决我的问题的方法。 我是 android 的新手,正在学习新的波士顿教程。 并且在 sharedpreferences activity 按钮上没有工作 click.showing 没什么也不例外没什么 我的代码是:

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>

像这样设置共享偏好;

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

删除所有异常捕获,除了隐藏正在发生的事情之外,这对您没有任何作用。您创建对话框但从未显示任何内容。

还有 post 你的布局,我预计,纯粹根据你的变量命名约定,R.id.tvLoad 是一个 TextView,你正试图将它分配给一个 EditText

如果是这样,请在布局中将 R.id.tvLoad 设为 EditText