I got a problem in my new android project. I cannot use the shared-preference with the intent starting. While i run my project it gets a force close and showing null-pointer exception in log-cat. Can anyone suggest anything to solve to this issue ? And here is my class
public class NextActivity extends Activity {
String a,b;
Context cntxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextlayout);
TextView et3 = (TextView)findViewById(R.id.textView3);
SharedPreferences prefs = cntxt.getSharedPreferences("myprefs", 0);
a = prefs.getString("KEY_FIRST","");
et3.setText(a);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent t = new Intent(cntxt,Kl_Activity.class);
startActivity(t);
}
// and get whatever type user account id is
});
}
}
Your error is at this line
SharedPreferences prefs = cntxt.getSharedPreferences("myprefs", 0);because you didnt init cntxt..
so just replace
or remove cntxt from line.