2017-04-13 531 views
1

我在解決方案下面放置here,得到一個Can not resolve方法put(java.lang.string,java.lang.string)我是試圖避免在方向變化時避免丟失我的webview中的數據,而無需手動處理方向更改。無法解析方法put(java.lang.string,java.lang.string)

我的代碼下面貼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myWebView = (WebView) findViewById(R.id.webcontent); 
    myWebView.getSettings().setJavaScriptEnabled(true); // enable javascript 
    myWebView.loadUrl("file:///android_asset/Welcome.html"); 
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    SharedPreferences prefs = context.getApplicationContext(). 
      getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE); 
    Editor edit = prefs.edit(); 
    edit.put("lastUrl",myWebView.getUrl()); 
    edit.commit(); // can use edit.apply() but in this case commit is better 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    if(myWebView != null) { 
     SharedPreferences prefs = context.getApplicationContext(). 
       getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE); 
     String s = prefs.getString("lastUrl",""); 
     if(!s.equals("")) { 
      myWebView.loadUrl(s); 
     } 
    } 
} 
+1

怎麼樣使用putString? https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putString(java.lang.String,%20java.lang.String) –

+0

有沒有這樣的類型'java.lang.string' 。 –

回答

3

編輯器不包含的方法 「放」。

因爲你想要把一個網址,你可以使用Editor.putString代替

所以你去

edit.putString("lastUrl",myWebView.getUrl()); 
+0

這是記錄在Javadocs,亞? –

+0

是的,在developer.android上 https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#putString(java.lang.String,java.lang.String) –

1

有用於SharedPreferences.Editor沒有put方法。 正確的應該是

edit.putString("lastUrl",myWebView.getUrl()); 

你可以找到更多在這裏SharedPreferences.Editor

相關問題