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

Java 共享首选项值未在Android中打印,java,android,sharedpreferences,Java,Android,Sharedpreferences,我想以共享首选项的形式将服务器IP和路径保存在android表单中,然后再由另一个java程序检索这些值。连接到服务器并下载文件。等等 因此,首先,我尝试在New.java中打印SharedReference值,这些值存储在Setting.java中,第一个java文件调用第二个文件,“Welcome”正在打印,但共享首选项中保存的值没有打印 以下是我的档案: Setting.java import android.app.Activity; import android.content.Int

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

因此,首先,我尝试在New.java中打印SharedReference值,这些值存储在Setting.java中,第一个java文件调用第二个文件,“Welcome”正在打印,但共享首选项中保存的值没有打印

以下是我的档案:

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

            }

        });

    }

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


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

            }

        });

    }

}
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
06-16 00:06:25.900:I/Adreno200 EGL(23644):局部修补程序:无
06-16 00:06:25.900:I/Adreno200 EGL(23644):重建分支:无
06-16 00:06:37.740:D/-heap(23644):GC_并发释放152K,4%释放8035K/8327K,暂停13ms+2ms,总计49ms
06-16 00:17:27.690:E/Trace(24124):打开跟踪文件时出错:没有此类文件或目录(2)
06-16 00:17:28.030:I/Adreno200 EGL(24124)::EGL 1.4高通公司版本:AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.043_msm7627a_JB_REL_RB1.2_Merge_REL_RB1.2_发布(Merge)
06-16 00:17:28.030:I/Adreno200 EGL(24124):建造日期:2013年1月29日星期二
06-16 00:17:28.030:I/Adreno200 EGL(24124):当地分行:
06-16 00:17:28.030:I/Adreno200 EGL(24124):远程分支:m/jb_rel_rb1.2
06-16 00:17:28.030:I/Adreno200 EGL(24124):局部贴片:无
06-16 00:17:28.030:I/Adreno200 EGL(24124):重建分支:无
06-16 00:17:36.120:D/-heap(24124):GC_并发释放174K,5%释放7985K/8327K,暂停17ms+2ms,总计40ms

在获取共享首选项时,将
模式\u WORLD\u READABLE
作为第二个参数传递

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

为什么你要使用SharedReferences而不是通过Intent to Settings活动传递ip值?有这么多java文件,我需要ip和路径值,而我的应用程序只需要一个用于设置ip和路径值的文件…你有任何错误吗?应用程序崩溃?Toast为空或显示错误的值?没有Toast显示所有这一切都没有做任何更改您在两个位置都做过这样的操作,即在共享首选项中保存值并将其保存到另一个文件中?在我这方面工作正常,如果您需要示例项目,若我使用文本视图打印,我可以发电子邮件给uMy,我的原始程序正在为我工作,但不是在toast中,所以问题无论如何都解决了。。。谢谢合作