因此,在一個活動中,我試圖將文件放入共享首選項中,然後在另一個活動中嘗試爲該字符串敬酒,但由於某種原因,android正在烘烤默認值,而不是我從共享首選項輸入的值。從共享首選項中檢索值時出錯
這裏是在MainActivity:
public void onPreviewFrame(byte[] data, Camera camera) {
// By default preview data is in NV21 format, if needed it must be converted
try {
Camera.Size previewSize = camera.getParameters().getPreviewSize();
int height = previewSize.height;
int width = previewSize.width;
ColorModelConverter converter = new ColorModelConverter(height, width);
int[] pixels = converter.convert(data, this.colorFormat);
int color = pickColor(pixels, height, width);
updateColorData(color);
storeColorInSharedPreferences(color);
Log.i("FRAME PREVIEW", "Color updated");
} catch (RuntimeException oops) {
// Do nothing, exception is thrown because onPreviewFrame is called after camera is released
Log.i("FRAME PREVIEW", "RuntimeException thrown into onPreviewFrame");
}
}
public void storeColorInSharedPreferences(int color) {
SharedPreferences sharedprefernces = getSharedPreferences("Mydata",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedprefernces.edit();
String first_control = String.valueOf(color);
editor.putString("first_control", first_control);
editor.commit();
這裏是第二個活動:
public class regressionlinecalculator extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regressionlinecalculator);
}
SharedPreferences sharedPreferences = getSharedPreferences("MyData",Context.MODE_PRIVATE);
String first_control = sharedPreferences.getString("first_control", "");
public void test(View view) {
Toast display_final_value = Toast.makeText(getApplicationContext(), new String(first_control), Toast.LENGTH_SHORT);
display_final_value.show();
}
}
測試是當我點擊我在XML文件中定義一個按鈕,只是會發生什麼。所以我的問題是爲什麼android會繼續烘烤默認值,即「」而不是顏色,這是我輸入到其他活動的共享首選項中的值?
在第一個活動有「MYDATA」並在第二個「我的數據」 – user2957782