So, i just want to save to a file when taping on a button, however, i have a FC, here is the code:
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String value1 = vol.getText().toString();
String value2 = kil.getText().toString();
if (value1 != null && value1.trim().length() > 0 && value2 != null && value2.trim().length() > 0)
{
float q1=Float.parseFloat(vol.getText().toString());
float q2=Float.parseFloat(kil.getText().toString());
float x=((q1 / q2)* 100);
String y= Float.toString(x);
cons.setText(y);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
data = format.format(new Date());
data = data + " : " + y + " L/100km\n" + value1 + " litres "+ value2 + " km\n";
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (data != "" ) {
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context, fileDir + fileName, data);
data = "";
}
}
else
{
Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_LONG).show();
}
}
});
Logcat: 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): FATAL EXCEPTION: main 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): java.lang.NullPointerException 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at carburant.android.com.carburant$1.onClick(carburant.java:118) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.view.View.performClick(View.java:2485) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.view.View$PerformClick.run(View.开发者_运维百科java:9080) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Handler.handleCallback(Handler.java:587) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Handler.dispatchMessage(Handler.java:92) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.os.Looper.loop(Looper.java:123) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invokeNative(Native Method) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at java.lang.reflect.Method.invoke(Method.java:507) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-16 20:52:59.080: ERROR/AndroidRuntime(1019): at dalvik.system.NativeStart.main(Native Method) 03-16 20:52:59.080: WARN/ActivityManager(68): Force finishing activity carburant.android.com/.carburant 03-16 20:52:59.854: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{406e89b8 carburant.android.com/.carburant}
Any help please ? Thanks :) .
According to your stack trace and extra information, the error is a null pointer at the line
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
I suspect that the context
parameter is null. Is context
a variable name which you've declared and assigned to a context already?
EDIT:
you need to assign a Context to your context variable. Just declaring it as a global variable without assigning it makes it null.
Please try this:
private Context context = getApplicationContext();
Well, I forgot to add this :
context = getApplicationContext();
精彩评论