Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReference上的方法类崩溃_Java_Android - Fatal编程技术网

Java SharedReference上的方法类崩溃

Java SharedReference上的方法类崩溃,java,android,Java,Android,因此,我有一个活动并调用另一个类,当它尝试获取共享首选项时,它就会崩溃 这是我在活动中呼叫的代码 Methods_play test1 = new Methods_play(); String[] tmp1 = test1.pewpew(level); 这里是我的方法类的代码 package import android.app.Activity; import android.content.SharedPreferences; import android.content.Share

因此,我有一个活动并调用另一个类,当它尝试获取共享首选项时,它就会崩溃

这是我在活动中呼叫的代码

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方法的调用):


由于
Methods\u play扩展了活动
您必须实现某些方法,例如
onCreate(Bundle myBundle)
。另外,
SharedReferences
需要一个
上下文
,因此您不能在
onCreate()
之前对其进行初始化,因为没有可用的
上下文
。试试像这样的东西

// 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在评论中指出的,您似乎不需要扩展
活动
。不要扩展任何内容,创建一个构造函数,将您的
活动上下文
传递给它,并使用它访问
共享引用

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();

撞车。怎样Stack?鉴于文章中的第一个代码块,我认为
Methods\u play
不需要是活动子类,OP只是这样做的,这样
getSharedReferences()
就可以解决这个问题。我认为这里可能需要一个带有上下文参数的构造函数。@MikeM。很有可能。我无法考虑每一种可能的情况。关键是,
SharedReferences
需要理解
上下文。不管怎么说,这篇评论更多的是针对OP的。我知道你知道你在做什么我认为迈克M.是对的,但我不知道他在说什么。我按照codeMagic说的做了,现在它在其他地方崩溃了,它说了一些关于上下文包装器的事情。(它在Integer tmp3=getResources().getInteger(getResources().getIdentifier(“播放器”+级别,“Integer”,getPackageName()));@user3254126上崩溃,需要使用正确的
上下文调用该调用
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();