所以我有一个Activity并调用另一个类,一旦它尝试获取共享首选项就崩溃了。
以下是我在Activity中调用的代码
Methods_play test1 = new Methods_play();
String[] tmp1 = test1.pewpew(level);
这里是我的方法类的代码
package
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class Methods_play extends Activity {
public String[] pewpew(int level) {
//GETS STUFF
Random rn = new Random();
!!!HERE IS THE ERROR!!! SharedPreferences prefs = getSharedPreferences(getResources().getString(R.string.preferences), MODE_PRIVATE);
Editor editor = prefs.edit();
//SOME CODE
//RETRUNS ARRAY
String ar[] = new String[2];
ar[0]= button1text;
ar[1]= button2text;
return ar;
}
}
我做错了什么?我已经在清单文件中声明了它(我没有,但它仍然崩溃了)。在正常活动上,获取共享首选项代码有效。
这是我第一次尝试在android中使用方法类,所以我很确定我搞砸了。
旁注:有没有关于此的好教程?
感谢您的帮助!
编辑(第23行是我说崩溃的地方,159是对方法的调用,143是对活动的oncreate方法中的defineqacall方法的调用):
09-03 01:36:01.742: E/AndroidRuntime(14001): FATAL EXCEPTION: main
09-03 01:36:01.742: E/AndroidRuntime(14001): Process: eu.jbr.quizexo, PID: 14001
09-03 01:36:01.742: E/AndroidRuntime(14001): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.jbr.quizexo/eu.jbr.quizexo.Career_Play}: java.lang.NullPointerException
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread.access$800(ActivityThread.java:144)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.os.Handler.dispatchMessage(Handler.java:102)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.os.Looper.loop(Looper.java:136)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread.main(ActivityThread.java:5146)
09-03 01:36:01.742: E/AndroidRuntime(14001): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 01:36:01.742: E/AndroidRuntime(14001): at java.lang.reflect.Method.invoke(Method.java:515)
09-03 01:36:01.742: E/AndroidRuntime(14001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
09-03 01:36:01.742: E/AndroidRuntime(14001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
09-03 01:36:01.742: E/AndroidRuntime(14001): at dalvik.system.NativeStart.main(Native Method)
09-03 01:36:01.742: E/AndroidRuntime(14001): Caused by: java.lang.NullPointerException
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
09-03 01:36:01.742: E/AndroidRuntime(14001): at eu.jbr.quizexo.Methods_play.defineqa(Methods_play.java:23)
09-03 01:36:01.742: E/AndroidRuntime(14001): at eu.jbr.quizexo.Career_Play.defineqacall(Career_Play.java:159)
09-03 01:36:01.742: E/AndroidRuntime(14001): at eu.jbr.quizexo.Career_Play.onCreate(Career_Play.java:143)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.Activity.performCreate(Activity.java:5231)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-03 01:36:01.742: E/AndroidRuntime(14001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
09-03 01:36:01.742: E/AndroidRuntime(14001): ... 11 more
答案 0 :(得分:0)
由于Methods_play extends Activity
您必须实施某些方法,例如onCreate(Bundle myBundle)
。此外,SharedPreferences
需要Context
,因此您无法在 onCreate()
之前将其初始化,因为没有Context
可用。尝试像
// declare it
SharedPreferences prefs;
public class Methods_play extends Activity {
public void onCreate(Bundle someBundle)
{
super.onCreate(someBundle);
//initialize it
prefs = getSharedPreferences(getResources().getString(R.string.preferences), MODE_PRIVATE);
正如MikeM在评论中指出的那样,您似乎不需要扩展Activity
。不要扩展任何内容,创建构造函数,将其传递给Activity Context
,然后使用它来访问SharedPreferences
。
public class Methods_play {
// create a member variable for your context
Context mContext;
//Create the constructor which takes a context
public void Methods_play(Context c)
{
mContext = c;
}
public String[] pewpew(int level) {
//GETS STUFF
Random rn = new Random();
// Use your context below
SharedPreferences prefs = mContext.getSharedPreferences(getResources()
.getString(R.string.preferences), MODE_PRIVATE);
Editor editor = prefs.edit();