【问题标题】:Shared Preference value not getting printed in Android共享首选项值未在 Android 中打印
【发布时间】:2013-06-15 18:55:21
【问题描述】:

我想以共享首选项在我的 android 表单中保存服务器 IP 和路径,然后想通过另一个 java prog 检索这些值。连接到服务器并下载文件。等等

所以,首先我尝试在 New.java 中打印 sharedpreference 值,这些值存储在 Setting.java 中,第一个 java 文件正在调用第二个并且 “欢迎” 正在打印,但共享首选项中保存的值不会打印。

以下是我的文件:

Setting.java

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Setting extends Activity {

    protected static final Toast NULL = null;
    Button btn1;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_page);


        btn1 = (Button) findViewById(R.id.btn1);
        final EditText  ip  = (EditText) findViewById(R.id.ip);
        final EditText  path  = (EditText)findViewById(R.id.path);





        btn1.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                SharedPreferences settings = getSharedPreferences("IP_PATH",0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("flag",true);
                editor.putString("IP",ip.getText().toString());
                editor.putString("PATH",path.getText().toString());
                editor.commit();






                Intent i = new Intent(Setting.this,New.class);
                //intent.putExtra("username",getIntent().getExtras().getString("username").toString());
                startActivity(i);

            }

        });

    }

}

New.java

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class New extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data);
       //System.out.println("Welcome to the home page !!");

        TextView tv = (TextView) findViewById(R.id.TextView01);
        tv.setText("Welcome");

        SharedPreferences prfs = getSharedPreferences("IP_PATH", 0);
        String s = prfs.getString("IP", "");

        Toast.makeText(New.this, s , Toast.LENGTH_LONG).show();
    }


}

错误:

06-16 00:06:25.900: I/Adreno200-EGL(23644): Local Patches: NONE
06-16 00:06:25.900: I/Adreno200-EGL(23644): Reconstruct Branch: NOTHING
06-16 00:06:37.740: D/-heap(23644): GC_CONCURRENT freed 152K, 4% free 8035K/8327K, paused 13ms+2ms, total 49ms
06-16 00:17:27.690: E/Trace(24124): error opening trace file: No such file or directory (2)
06-16 00:17:28.030: I/Adreno200-EGL(24124): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.043_msm7627a_JB_REL_RB1.2_Merge_release_AU (Merge)
06-16 00:17:28.030: I/Adreno200-EGL(24124): Build Date: 01/29/13 Tue
06-16 00:17:28.030: I/Adreno200-EGL(24124): Local Branch: 
06-16 00:17:28.030: I/Adreno200-EGL(24124): Remote Branch: m/jb_rel_rb1.2
06-16 00:17:28.030: I/Adreno200-EGL(24124): Local Patches: NONE
06-16 00:17:28.030: I/Adreno200-EGL(24124): Reconstruct Branch: NOTHING
06-16 00:17:36.120: D/-heap(24124): GC_CONCURRENT freed 174K, 5% free 7985K/8327K, paused 17ms+2ms, total 40ms

reference

【问题讨论】:

  • 你为什么使用 SharedPreferences 而不是通过 Intent 将 ip 值传递给设置活动?
  • 我需要IP和Path值的java文件太多了,而我的应用程序只需要用于设置IP和Path值一个...
  • 你有什么错误吗?应用崩溃了?
  • Toast 为空或显示错误值?
  • 没有吐司出现

标签: java android sharedpreferences


【解决方案1】:

在获取共享首选项时将MODE_WORLD_READABLE 作为第二个参数传递。

SharedPreferences settings = getSharedPreferences("IP_PATH",MODE_WORLD_READABLE);

【讨论】:

  • 这没有做任何改变
  • 您是否在两个地方都这样做了,即在共享首选项中保存价值以及在另一个文件中获取价值?
  • 它工作正常,如果你需要示例项目,我可以给你发电子邮件
  • 如果我使用 textview 打印,我的原始程序对我有用,但不是在 toast 中,所以无论如何问题都解决了......感谢您的合作
猜你喜欢
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多