getSharedPrefernces仅在一个在另一个活动中正常工作的活动中返回null

时间:2016-11-14 05:47:42

标签: java android android-sharedpreferences

我使用以下代码保存共享偏好。

sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.putString("NIC",nic);
editor.putString("fName",fName);
editor.putString("mName",mName);
editor.putString("lName",lName);
editor.putString("aNo",aNo);
editor.putString("lane",aLane);
editor.putString("city",aCity);
editor.putString("contactNo",contactNo);
editor.putString("regDate",regDate);
editor.putString("cardNo",cardNo);
editor.putString("balance",balance);
editor.commit();

使用以下代码获取Profile活动中的共享首选项,并且它正常工作。

private void getPrefs() {
        SharedPreferences userDetails = Profile.this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        id = userDetails.getString("NIC", "");
        f_Name = userDetails.getString("fName", "");
        m_Name = userDetails.getString("mName", "");
        l_Name = userDetails.getString("lName", "");
        contact_no = userDetails.getString("contactNo", "");
        a_No = userDetails.getString("aNo", "");
        a_Lane = userDetails.getString("lane", "");
        a_City = userDetails.getString("city", "");
        card_No = userDetails.getString("cardNo", "");
        reg_Date = userDetails.getString("regDate", "");
    }

但是当我在另一个活动中使用相同的内容时,它会为id返回null。

private void getPrefs() {
        SharedPreferences userDetails = Travel.this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        id = userDetails.getString("NIC", "");
}

mainfest.xml

<activity
            android:name=".Profile"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.pavsaranga.scat.PROFILE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Travel"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="com.example.pavsaranga.scat.TRAVEL" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

任何人都可以帮我解决这个问题吗? 旅行活动的完整代码

public class Travel extends AppCompatActivity {

    TableLayout tl;
    StringBuilder sb;
    String id, result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.travel);
        tl = (TableLayout)findViewById(R.id.tlTravel);
        getPrefs();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                getResponse();
            }
        });
        t1.start();
    }

    private void getPrefs() {
        SharedPreferences userDetails = getApplicationContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        id = userDetails.getString("NIC", "");
    }}

1 个答案:

答案 0 :(得分:0)

将此或Profile.this更改为declare l_blob blob; begin null; end;

喜欢:

getApplicationContext()