i am working on android and trying to read a value from my preference file.
but when i run.a message appears in emulator.the
the application has stopped unexpectedly
here is my preference file name pref.and it is inres/xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:defaultValue="false"
android:key="@string/first"/>
</PreferenceScreen>
this is main activity where i am reding the value.
package code.finalwork;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class FinalWorkActivity extends Activity {
private String pref_file="pref.xml";
EditText text=(EditText) findViewById(R.id.editText1);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences pref=getSharedPreferences(pref_file,Context.MODE_PRIVATE);
Boolean val= pref.getBoolean("firstuse", true);
if(val)
{
text.setText("first use");
}
else
{
text.setText("Not first use");
}
}
}
please someone figure out the problem.
Try this:
If it doesn’t fix please post the error log.
EDIT: remember that so save preferences you need to do: