我有以下活动扩展了ListActivity:
public class TweetActivity extends ListActivity {
SharedPreferences settings = this.getSharedPreferences("NewsTweetSettings", 0);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
}
}
我设置sharePreferences的行总是导致应用程序在模拟器中崩溃,我找不到原因。有什么想法吗?
答案 0 :(得分:4)
在调用onCreate()超类实现后使用oncreate()中的行。它可能会帮助你
public class TweetActivity extends ListActivity {
SharedPreferences settings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings=this.getSharedPreferences("NewsTweetSettings", 0);
....
}
}
原因是我们在行super.onCreate(savedInstanceState);
快乐编码
答案 1 :(得分:2)
在您的this.getSharedPreferences("NewsTweetSettings", 0);
中调用此方法OnCreate
,如
public class TweetActivity extends ListActivity {
SharedPreferences settings;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings=this.getSharedPreferences("NewsTweetSettings", 0);
....
}
}