如何在Service类中使用齐射?

时间:2015-09-01 09:49:45

标签: android android-volley

我正在尝试向url发送请求,并且在成功响应时返回一个布尔值。我做错了什么?一旦我安装了应用程序,就会在下一行遇到NullPointerExeption。

/ * private final RequestQueue reQueue=Volley.newRequestQueue(this); * /

package com.exampfle.real;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class RealService extends Service{

    private static final String TAG="RealService";
    private boolean isRunning=false;
    private IBinder mBinder=new MyBinder();
    private boolean intenetAccess=false;
/* Geting NullPointerException at following line */
    private final RequestQueue   reQueue=Volley.newRequestQueue(this);
    private final String url="http://www.google.com";

    public boolean SendRequest()
    {
        StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
                url, 
                new Response.Listener<String>() {

            @Override
            public void onResponse(
                    String response) {

                intenetAccess=true;
            }
        },

        new Response.ErrorListener() {

            @Override
            public void onErrorResponse(
                    VolleyError error) {

                intenetAccess=false;

            }
        });

        try{
            reQueue.add(request);
        }
        catch(Exception e){}

        return intenetAccess;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "Service onCreate");

        isRunning=true;

    }



    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "Service onBind");
        return mBinder;
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(TAG, "Service onRebind");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "Service onUnBind");
        return true;
    }

    @Override
    public void onDestroy() {

    isRunning=false;

        Log.i(TAG, "Service onDestroy");
        super.onDestroy();
    }



    public class MyBinder extends Binder
    {
        RealService getService()
        {
            return RealService.this;
        }
    }
}

1 个答案:

答案 0 :(得分:5)

您使用 this 关键字,但该对象未实例化。在构造函数中移动该行

 package com.exampfle.real; 

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class RealService extends Service{

    private static final String TAG="RealService";
    private boolean isRunning=false;
    private IBinder mBinder=new MyBinder();
    private boolean intenetAccess=false;
/* Change here */ 
    private RequestQueue reQueue;
    private final String url="http://www.google.com";

    public boolean SendRequest() 
    {
/* Change here */
    reQueue = Volley.newRequestQueue(this); 
        StringRequest request=new StringRequest(com.android.volley.Request.Method.GET,
                url, 
                new Response.Listener<String>() {

            @Override 
            public void onResponse( 
                    String response) {

                intenetAccess=true;
            } 
        }, 

        new Response.ErrorListener() {

            @Override 
            public void onErrorResponse( 
                    VolleyError error) {

                intenetAccess=false;

            } 
        }); 

        try{ 
            reQueue.add(request);
        } 
        catch(Exception e){}

        return intenetAccess;

    } 

    @Override 
    public void onCreate() { 
        super.onCreate(); 
        Log.i(TAG, "Service onCreate");

        isRunning=true;

    } 



    @Override 
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "Service onBind");
        return mBinder;
    } 

    @Override 
    public void onRebind(Intent intent) {
        Log.i(TAG, "Service onRebind");
        super.onRebind(intent);
    } 

    @Override 
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "Service onUnBind");
        return true; 
    } 

    @Override 
    public void onDestroy() { 

    isRunning=false;

        Log.i(TAG, "Service onDestroy");
        super.onDestroy(); 
    } 



    public class MyBinder extends Binder
    { 
        RealService getService() 
        { 
            return RealService.this;
        } 
    } 
}