Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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 发出截击okhttp请求时出现NullPointerException_Java_Android_Android Volley_Okhttp - Fatal编程技术网

Java 发出截击okhttp请求时出现NullPointerException

Java 发出截击okhttp请求时出现NullPointerException,java,android,android-volley,okhttp,Java,Android,Android Volley,Okhttp,我正在尝试使用谷歌截击和okHttp。我将按照本教程进行设置。我安排了单打和单打。使用了他们的get string请求,但每次我发出请求时都会得到NullPointerException 7717-8280/com.admin.zipline E/AndroidRuntime﹕ FATAL EXCEPTION: pool-9-thread-1 Process: com.admin.zipline, PID: 7717 java.lang.NullPointerException

我正在尝试使用谷歌截击和okHttp。我将按照本教程进行设置。我安排了单打和单打。使用了他们的get string请求,但每次我发出请求时都会得到NullPointerException

7717-8280/com.admin.zipline E/AndroidRuntime﹕ FATAL EXCEPTION: pool-9-thread-1
Process: com.admin.zipline, PID: 7717
java.lang.NullPointerException
        at com.admin.zipline.activities.AccountVerification.getTransactionsDetails(AccountVerification.java:295)
        at com.admin.zipline.activities.AccountVerification_.access$701(AccountVerification_.java:25)
        at com.admin.zipline.activities.AccountVerification_$8.execute(AccountVerification_.java:203)
        at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:302)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
这是我的要求


错误出现在第295行,即
VolleyNetwork.getInstance().addToRequestQueue(streq,tag_string_req)

@Background
public void getTransactionsDetails(){
字符串url=NetworkConstants.url\u事务;
//用于取消请求的标记
String tag_String_req=“String_req”;
Log.i(“URL”,URL);
StringRequest strReq=新的StringRequest(Request.Method.GET,
url,新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.i(“Response”,Response.toString());
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.i(“Error”,“Error:+Error.getMessage());
}
}){
/**
**传递一些请求头
* */
@凌驾
公共映射getHeaders()引发AuthFailureError{
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json”);
标题.put(“授权”,截击网络.Authorization);
返回标题;
}
};
//将请求添加到请求队列
if(VolleyNetwork.getInstance()==null){
Log.i(“空”、“网络空”);
}否则{
getInstance().addToRequestQueue(streq,tag_string_req);
}
}
我的singleton设置几乎完全相同,只是在顶部添加了一些全局变量

VolleyNetwork.java

public class VolleyNetwork extends Application {
Context context;
public static String access_token,token_type,user_id,name, authorization;
public VolleyNetwork(Context context) {
    this.context = context;
    SharedPreferences preferences = context.getSharedPreferences(FileNames.login_details, Context.MODE_PRIVATE);
    access_token = preferences.getString(NetworkConstants.acess_token, "");
    token_type = preferences.getString(NetworkConstants.token_type, "");
    user_id = preferences.getString(NetworkConstants.user_id, "");
    authorization = token_type + " " + access_token;
}

public static final String TAG = VolleyNetwork.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static VolleyNetwork mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized VolleyNetwork getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

}
公共类截击网络扩展应用{
语境;
公共静态字符串访问令牌、令牌类型、用户id、名称、授权;
公共截击网(上下文){
this.context=上下文;
SharedReferences首选项=context.getSharedReferences(FileNames.login\u详细信息,context.MODE\u PRIVATE);
access_token=preferences.getString(NetworkConstants.access_token,“”);
token_type=preferences.getString(NetworkConstants.token_type,“”);
user\u id=preferences.getString(NetworkConstants.user\u id,“”);
授权=令牌\类型+“”+访问\令牌;
}
公共静态最终字符串标记=VolleyNetwork.class
.getSimpleName();
私有请求队列mRequestQueue;
私有图像加载器;
专用静态截击网;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步截击网络getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共ImageLoader getImageLoader(){
getRequestQueue();
if(mImageLoader==null){
mImageLoader=新的ImageLoader(this.mRequestQueue,
新的LruBitmapCache());
}
返回此.mImageLoader;
}
公共无效addToRequestQueue(请求请求,字符串标记){
//如果标记为空,则设置默认标记
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}
我想这可能和我的舱单有关。我真的不知道如何设置。我之前的人安装了大部分应用程序,而我对Java不太熟悉

现在清单使用的是
android.permission.internetpermission
,应用程序的名称是
.activities.MyApplication
。MyApplication扩展了应用程序。我试图将应用程序名设置为
.network.VolleyNetwork
,并让VolleyNetwork扩展我的应用程序,但得到了“VolleyNetwork没有默认构造函数”


我不知道该怎么办

之所以会发生这种情况,是因为您实际上并没有创建对象凌空网络

单例模式的工作原理如下:

public class MySingleton {
    private static MySingleton instance;
    private MySingleton(){
        //constructor here
    }
    public static MySingleton getInstance(){
        if(instance==null){
            instance = new MySingleton();
        }
        return instance;
    }
}
请注意,static
getInstance
方法实际上创建了您的实例,并确保它保存在static字段中,而在您的示例中,它检索默认值null


另外,请确保将构造函数设置为私有,以便只能通过public
getInstance
方法检索实例,否则任何人都可以调用构造函数并创建任意数量的实例。

哪一行是295,错误是?
VolleyNetwork.getInstance().addToRequestQueue()(strReq,tag_string_req);
这是第295行,忘了提及,抱歉。也更新了原始问题。嗯。我不确定何时调用
onCreate()
,但是否可能
mInstance()
为空,因此
VolleyNetwork.getInstance()呢
返回空值的是什么?我们需要从AccountVerification.class的GetTransactionDetails()函数中获取代码。它在那里发生异常您的问题是由于
VolleyNetwork.getInstance()
,它返回空值,因此对空对象调用函数会引发空指针异常。。。
public class MySingleton {
    private static MySingleton instance;
    private MySingleton(){
        //constructor here
    }
    public static MySingleton getInstance(){
        if(instance==null){
            instance = new MySingleton();
        }
        return instance;
    }
}