简体   繁体   English

共享首选项值未在Android中打印

[英]Shared Preference value not getting printed in Android

I want to save server IP and path in my android form in Shared preference and later on want to retrieve those values by another java prog. 我想以“共享”首选项将服务器IP和路径保存在我的android表单中,以后又想通过另一个java编程序检索这些值。 to connect to server and download files. 连接到服务器并下载文件。 etc. 等等

So, first i tried to print the sharedpreference values in New.java , where the values are stored in Setting.java , the 1st java file is calling the second and "Welcome" is getting printed, but the saved values in shared preferences in not getting printed. 所以,首先我试图打印New.java,当值存储在Setting.java的sharedpreference值,1号的java文件拨打第二个,“欢迎光临”是越来越打印,但在不共享偏好保存的值被打印。

Following are my files: 以下是我的文件:

Setting.java 设置.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 新的.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();
    }


}

Errors: 错误:

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 参考

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM