使用共享首选项进行存储

时间:2016-03-09 18:10:33

标签: android sharedpreferences

我正在制作一个速度计,想要一个最高速度的文本视图,看看手机的速度有多快,我尝试使用共享首选项,但它在启动时崩溃了。我已将共享首选项放在runnable中,以便快速更新并检查当前速度是否高于最高速度,如果它存储它并显示在文本视图中。谢谢Ben。

import android.app.*;
import android.os.*;
import android.location.*;
import android.content.*;
import android.widget.*;
import java.text.*;
import android.graphics.*;
import android.graphics.drawable.*;
import android.view.*;

public class MainActivity extends Activity implements LocationListener
{

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

SharedPreferences prefs;
String topspeed;

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

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLACK));

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

this.onLocationChanged(null);

SharedPreferences prefs = this.getSharedPreferences("values",     Context.MODE_PRIVATE);
String topspeed = prefs.getString("topspeed", "0");
TextView tv3 = (TextView) this.findViewById(R.id.tv3);
tv3.setText(topspeed);
}




@Override
public void onLocationChanged(Location p1)
{
TextView tv1 = (TextView) this.findViewById(R.id.mainTextView1);

if(p1 == null){
tv1.setText("-.- mp/h");
}
else{
 float currentspeed = p1.getSpeed();
double mph_conversion = currentspeed * 2.2369362920544;

DecimalFormat precision = new DecimalFormat("0.0");

tv1.setText(precision.format(mph_conversion) + " mp/h");

double topspeed1 = Double.parseDouble(topspeed);

if(topspeed1 > mph_conversion){
SharedPreferences prefs = this.getSharedPreferences("values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("topspeed", Double.toString(mph_conversion));
editor.apply();
}
else{
TextView tv3 = (TextView) this.findViewById(R.id.tv3);
SharedPreferences prefs = this.getSharedPreferences("values", Context.MODE_PRIVATE);
String topspeed = prefs.getString("topspeed", "0");
tv3.setText(topspeed);

}

}
}

@Override
public void onStatusChanged(String p1, int p2, Bundle p3)
{
// TODO: Implement this method
}

@Override
public void onProviderEnabled(String p1)
{
// TODO: Implement this method
}

@Override
public void onProviderDisabled(String p1)
{
TextView tv1 = (TextView) this.findViewById(R.id.mainTextView1);
tv1.setText("Turn on high GPS accuracy");
}

private Runnable updateTimerThread = new Runnable() {

public void run() {


timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
   + String.format("%02d", secs) + ":"
   + String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}

};


public void bgcolour (View view){
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));

}


public void tv2click (View v){
//start timer
timerValue = (TextView) findViewById(R.id.timerValue);
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}

@Override
public void onBackPressed() {

android.os.Process.killProcess(android.os.Process.myPid());
 // This above line close correctly
}


}

Logcat错误 03-09 20:57:29.690 E / AndroidRuntime(23288):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example / com.example.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'关于空对象引用的android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String,int)'

2 个答案:

答案 0 :(得分:0)

如果您要立即阅读存储的偏好设置,请不要使用apply()。请改用commit()。此调用在主线程上运行,而不是像apply()这样的背景,但如果您只是存储一小段数据,则不应该注意到任何差异。这是关于它的更多信息。 SharedPreferences.Editor

答案 1 :(得分:0)

在Activity上调用构造函数之前,不能运行这两行:

SharedPreferences prefs = this.getSharedPreferences("values", Context.MODE_PRIVATE);
String topspeed = prefs.getString("topspeed", "0");

您可以修改它,例如,在此处定义变量,但在onCreate()方法中设置它们。