Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Android 具有上下文的NullpointerException_Android_Android Asynctask_Nullpointerexception_Sharedpreferences_Android Context - Fatal编程技术网

Android 具有上下文的NullpointerException

Android 具有上下文的NullpointerException,android,android-asynctask,nullpointerexception,sharedpreferences,android-context,Android,Android Asynctask,Nullpointerexception,Sharedpreferences,Android Context,我试图在异步任务中获得共享首选项,但我没有得到它 我必须如何在这里使用上下文 package com.example.wettkampftimerbt; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.URL; import android.content.Context; import android.content.SharedPref

我试图在异步任务中获得共享首选项,但我没有得到它

我必须如何在这里使用上下文

package com.example.wettkampftimerbt;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;

public class network extends AsyncTask<URL, String, String> {

    private final String PREFS_NAME = "prefs", W1 = "w1",  W2="w2", W3="w3", W4="w4", 
            W5="w5", W6="w6", W7="w7", SENDBEG="sendbeg", SENDEND="sendend";
    int w1, w2, w3, w4, w5, w6, w7, sendend, sendbeg;

    protected String doInBackground(URL... params) {
        int c2, c3, c4, c5, c6;
        int w0 = 00;
        try {
            w1 = sendbeg;
            c2 = w2;
            c3 = w3;
            c4 = w4;
            c5 = w5;
            c6 = w6;
            w7 = sendend;

            Socket ss = new Socket("192.168.3.100", 4455);
            System.out.println(ss);
            boolean stopData = true;
            System.out.println("lane1 stop send");
            DataOutputStream dos = new DataOutputStream(
                    ss.getOutputStream());
            while (stopData) {
                dos.writeByte(w0);
                dos.writeByte(w1);
                dos.writeByte(c2);
                dos.writeByte(c3);
                dos.writeByte(c4);
                dos.writeByte(c5);
                dos.writeByte(c6);
                dos.writeByte(w7);
                stopData=false;
            }
        } catch (IOException e) {
            System.out.println("IO error" + e);
            e.printStackTrace();
        }

        return "Done";
    }

    public void onPreExecute(){
        Context context = network.this;
        SharedPreferences prefs = context.getSharedPreferences("PREFS_NAME");

        doInBackground();
    }

    public void getPrefs (Context context){         
        w1= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W1, "00"));
        w2= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W2, "00"));
        w3= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W3, "00"));
        w4= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W4, "00"));
        w5= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W5, "00"));
        w6= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W6, "00"));
        w7= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(W7, "00"));
        sendbeg= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(SENDBEG, "00"));
        sendend= Integer.valueOf(context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).getString(SENDEND, "00"));
    }
}
package com.example.wettkampftimerbt;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.net.Socket;
导入java.net.URL;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.os.AsyncTask;
公共类网络扩展异步任务{
私有最终字符串PREFS_NAME=“PREFS”,W1=“W1”,W2=“W2”,W3=“W3”,W4=“W4”,
W5=“W5”,W6=“W6”,W7=“W7”,sendberg=“sendberg”,SENDEND=“SENDEND”;
int w1、w2、w3、w4、w5、w6、w7、发送端、发送端;
受保护的字符串doInBackground(URL…参数){
int c2、c3、c4、c5、c6;
int w0=00;
试一试{
w1=森贝格;
c2=w2;
c3=w3;
c4=w4;
c5=w5;
c6=w6;
w7=发送端;
插座ss=新插座(“192.168.3.100”,4455);
系统输出打印LN(ss);
布尔stopData=true;
System.out.println(“lane1停止发送”);
DataOutputStream dos=新的DataOutputStream(
ss.getOutputStream());
while(stopData){
dos.writeByte(w0);
dos.writeByte(w1);
dos.writeByte(c2);
dos.writeByte(c3);
dos.writeByte(c4);
写字节(c5);
dos.writeByte(c6);
dos.writeByte(w7);
stopData=false;
}
}捕获(IOE异常){
系统输出打印项次(“IO错误”+e);
e、 printStackTrace();
}
返回“完成”;
}
公共无效onPreExecute(){
Context=network.this;
SharedReferences prefs=context.getSharedReferences(“prefs_NAME”);
doInBackground();
}
public void getPrefs(上下文){
w1=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w1,“00”);
w2=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w2,“00”);
w3=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w3,“00”);
w4=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w4,“00”);
w5=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w5,“00”);
w6=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w6,“00”);
w7=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(w7,“00”);
sendbeg=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(sendbeg,“00”);
sendend=Integer.valueOf(context.getSharedReferences(PREFS_NAME,context.MODE_PRIVATE).getString(sendend,“00”);
}
}
这是一个代码片段,我的问题是:我没有错误,除了启动时出现的nullPointerException(由
getPrefs(null);
?)引起)。有人能帮我吗

我已经阅读了很多上下文解释,甚至是我访问过的Android开发者网站,但我不知道上下文是什么,它到底做了什么

编辑:谢谢你的快速回答,但它仍然不起作用。我现在把整个活动放在这里,你给了我一个样本,但我现在得到了错误。是由我决定,还是Eclipse恨我

EDIT2:我自己给doInBackground打电话,因为它不是由他自己开始的

EDIT3:现在已经解决了,它可以工作了^^我直接从我的主要活动调用getPrefs,然后从getPrefs调用网络。。。到目前为止还不错,但是:我得到了这个NetworkOnMainThreadException错误。现在该怎么办?

试试这个:

1) 在异步类中定义一个局部变量(在您的示例中是网络类)

2) 在异步任务中创建构造函数以接受上下文:

public network(Context context) {
    this.context = context;       
}
3) 现在,在
onPreExecute()
中调用
getPrefs
时,按如下方式调用:

getPrefs(context);
4) 您可以从MainActivity调用此类,如下所示:

network networkObj= new network(MainActivity.this);
networkObj.execute(url);

您必须将类的上下文提供给
getPrefs()
。 使用
getPrefs(yourActivity.this)
有两个选项:

1。将AsyncTask定义为活动的内部类
AsyncTask类必须是静态的,才能正常工作

public class MyActivity extends Activity {
    ...    

    private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
        ...

        public void onPreExecute() {
            Context context = MyActivity.this;
            SharedPreferences prefs = context.getSharedPreferences("PREFS_NAME");
        }
    }

}
公共类MyActivity扩展活动{
...    
私有类MyAsyncTask扩展了AsyncTask{
...
公共无效onPreExecute(){
Context=MyActivity.this;
SharedReferences prefs=context.getSharedReferences(“prefs_NAME”);
}
}
}
2。将活动/上下文传递给AsyncTask构造函数,并维护对其的WeakReference
AsyncTask类应该是静态的。不要只维护对活动/上下文的硬引用(除非是应用程序上下文),否则您将泄漏内存:

public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    private WeakReference<Context> mContextRef;

    public MyAsyncTask(Context context) {
        mContextRef = new WeakReference<Context>(context);
    }

    ...

    public void onPreExecute() {
        Context context = mContextRef.get();
        // context may be null if the activity has been destroyed
        if (context != null) {
            SharedPreferences prefs = context.getSharedPreferences("PREFS_NAME");
        }
    }
}
公共类MyAsyncTask扩展了AsyncTask{
私有WeakReference mContextRef;
公共MyAsyncTask(上下文){
mContextRef=新的WeakReference(上下文);
}
...
公共无效onPreExecute(){
Context=mContextRef.get();
//如果活动已销毁,则上下文可能为空
if(上下文!=null){
SharedReferences prefs=context.getSharedReferences(“prefs_NAME”);
}
}
}

getPrefs(空)???????????(是的,这是很多?但我真的很困惑)这就是eclipsed用ctrl+1 oo对我说的,你不认为当你试图访问它时会导致,比如说,nullpointerexception吗?我就是这么想的。。。正因为如此,我特别提到了它^^
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {

    private WeakReference<Context> mContextRef;

    public MyAsyncTask(Context context) {
        mContextRef = new WeakReference<Context>(context);
    }

    ...

    public void onPreExecute() {
        Context context = mContextRef.get();
        // context may be null if the activity has been destroyed
        if (context != null) {
            SharedPreferences prefs = context.getSharedPreferences("PREFS_NAME");
        }
    }
}