簡體   English   中英

共享首選項值未在Android中打印

[英]Shared Preference value not getting printed in Android

我想以“共享”首選項將服務器IP和路徑保存在我的android表單中,以后又想通過另一個java編程序檢索這些值。 連接到服務器並下載文件。 等等

所以,首先我試圖打印New.java,當值存儲在Setting.java的sharedpreference值,1號的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);

            }

        });

    }

}

新的.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

參考

獲取共享首選項時,將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