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

时间:2015-03-05 11:39:55

标签: android sharedpreferences

我已搜索过但未找到问题的解决方案。 我是Android的新手,并关注新的波士顿教程。 并且在共享偏好中,活动按钮不在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>

2 个答案:

答案 0 :(得分:0)

像这样建立共享偏好;

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

答案 1 :(得分:0)

删除所有异常捕获,除了隐藏正在发生的事情之外,这对您没有任何帮助。您创建对话框时不会显示任何内容。

同样发布您的布局,我希望,纯粹根据您的变量命名惯例,R.id.tvLoadTextView,并且您尝试将其分配给EditText

如果是这种情况,请在布局中设置R.id.tvLoad EditText