使用凌空库的问题

时间:2016-09-30 17:52:56

标签: java android

我是这个图书馆的新手,我遇到了一些问题。 我在AppController类中使用它。 AppController.java:

package ir.naserpour.ketabsara;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by reza-pc on 9/30/16.
 */
public class AppController extends Application {

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

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

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

    public static synchronized AppController 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);
        }
    }
}

和我的MainActivity.java文件是这样的:

package ir.naserpour.ketabsara;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.*;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Locale;

import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Configuration configuration = getResources().getConfiguration();
        configuration.setLayoutDirection(new Locale("fa"));
        getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
        setContentView(R.layout.activity_main);
        //typeface
        final Typeface Font = Typeface.createFromAsset(getAssets(),"fonts/sans.ttf");

        //category1
        String cat1url= "http://data.h70.ir/category1.php";
        HorizontalScrollView cat1scroll = (HorizontalScrollView)findViewById(R.id.category1);
        ViewGroup.LayoutParams Sparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        cat1scroll.setLayoutParams(Sparams);
        final LinearLayout hlayout = new LinearLayout(getApplicationContext());
        ViewGroup.LayoutParams Lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
        hlayout.setOrientation(LinearLayout.HORIZONTAL);
        hlayout.setLayoutParams(Lparams);
        cat1scroll.addView(hlayout);
        final ProgressDialog pdialog = new ProgressDialog(MainActivity.this);
        pdialog.setMessage("در حال دریافت اطلاعات...");
        pdialog.setCancelable(false);
        pdialog.show();
        JsonArrayRequest req = new JsonArrayRequest(cat1url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for (int i=0;i<= response.length();i++){
                            try {
                                JSONObject cat1object= response.getJSONObject(i);
                                String name = cat1object.getString("name");
                                String image = cat1object.getString("image");
                                create_layout(name, image, hlayout);


                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(),"cat1object",Toast.LENGTH_SHORT).show();
                            }

                        }
                        pdialog.hide();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_SHORT).show();
                pdialog.hide();
            }
        });
        AppController.getInstance().addToRequestQueue(req);
    }
    public void create_layout(String name, String image, LinearLayout hlayout){

        CardView card = new CardView(MainActivity.this);
        ViewGroup.LayoutParams card_params = new ViewGroup.LayoutParams(200,300);
        card.setLayoutParams(card_params);
        NetworkImageView image_view = new NetworkImageView(MainActivity.this);
        ImageLoader loader = AppController.getInstance().getImageLoader();
        image_view.setImageUrl(image, loader);
        ViewGroup.LayoutParams image_params = new ViewGroup.LayoutParams(200,240);
        image_view.setLayoutParams(image_params);
        card.addView(image_view);
        TextView name_view = new TextView(MainActivity.this);
        ViewGroup.LayoutParams textparams = new ViewGroup.LayoutParams(200,60);
        name_view.setLayoutParams(textparams);
        name_view.setGravity(Gravity.CENTER);
        name_view.setText(name);
        card.addView(name_view);
        hlayout.addView(card);

    }

}

但在logcat中我看到MainActivity的以下行中存在NullPointerException并导致应用程序崩溃:(第89行)

AppController.getInstance().addToRequestQueue(req);

和logcat错误详细信息如下:

09-30 22:03:07.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:08.014 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:11.024 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:12.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:14.044 1716-1716/? E/wpa_supplicant: RSSI=-24;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:17.054 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:17.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:20.074 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:21.654 1177-1255/? E/SmartHeartBeat: HeartBeatNetwork:sendData failed
09-30 22:03:21.664 8426-8567/? E/ActivityThread: Failed to find provider info for com.huawei.android.hicloud.loginProvider
09-30 22:03:21.804 1177-1255/? E/SmartHeartBeat: HeartBeatNetwork:sendData failed
09-30 22:03:21.844 8426-8567/? E/ActivityThread: Failed to find provider info for com.huawei.android.hicloud.loginProvider
09-30 22:03:22.374 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:23.084 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:23.234 2024-2024/? E/HwSystemManager: AppCleanUpService:msg is 1
09-30 22:03:23.684 1497-1497/? E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
09-30 22:03:23.844 3044-3044/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.844 3044-3044/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.844 3044-3044/? E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.844 3044-3044/? E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.864 3044-3044/? E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.874 3044-3044/? E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.874 3044-3044/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.884 3044-3044/? E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.904 3044-3044/? E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.904 3044-3044/? E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:23.914 3044-3044/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:24.184 3044-3044/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: ir.naserpour.ketabsara, PID: 3044
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.naserpour.ketabsara/ir.naserpour.ketabsara.MainActivity}: java.lang.NullPointerException
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2275)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2325)
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:136)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5352)
                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:863)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                     at dalvik.system.NativeStart.main(Native Method)
                                                  Caused by: java.lang.NullPointerException
                                                     at ir.naserpour.ketabsara.MainActivity.onCreate(MainActivity.java:89)
                                                     at android.app.Activity.performCreate(Activity.java:5280)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2325) 
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:136) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5352) 
                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                     at java.lang.reflect.Method.invoke(Method.java:515) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:863) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
                                                     at dalvik.system.NativeStart.main(Native Method) 
09-30 22:03:24.564 1602-1602/? E/NetworkScheduler.SR: Invalid parameter app
09-30 22:03:24.564 1602-1602/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
09-30 22:03:24.634 2528-3702/? E/Drive.UninstallOperation: Package still installed ir.naserpour.ketabsara
09-30 22:03:24.844 2024-2024/? E/HwSystemManager: AppCleanUpService:msg is 0
09-30 22:03:24.964 2528-3188/? E/IntentOperationSvc: Failed to instantiate Chimera operation impl, dropping operation
09-30 22:03:25.074 5100-5100/? E/Finsky: [1] com.google.android.finsky.wear.bo.a(838): onConnectionFailed: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
09-30 22:03:25.704 1602-1602/? E/NetworkScheduler.SR: Invalid parameter app
09-30 22:03:25.704 1602-1602/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
09-30 22:03:26.094 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:27.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:29.094 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:30.494 249-994/? E/CommandListener: readDefRoute call readDefRoute

                                                [ 09-30 22:03:30.494   249:  994 E/         ]
                                                isWifiDefaultRouteExisted /system/bin/ip route
09-30 22:03:30.534 1177-1260/? E/InputDispatcher: channel '423bb8d8 ir.naserpour.ketabsara/ir.naserpour.ketabsara.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
09-30 22:03:31.514 249-994/? E/CommandListener: readDefRoute Rsp ok
09-30 22:03:31.514 1177-1177/? E/NetdConnector: NDC Command {218 route readDefRoute} took too long (1013ms)
09-30 22:03:32.114 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:32.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:35.124 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:37.014 1497-1497/? E/HwLauncher: Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
09-30 22:03:37.044 3633-3633/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.044 3633-3633/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.044 3633-3633/? E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.044 3633-3633/? E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.054 3633-3633/? E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.054 3633-3633/? E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.054 3633-3633/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.064 3633-3633/? E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.074 3633-3633/? E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.074 3633-3633/? E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.084 3633-3633/? E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method ir.naserpour.ketabsara.MainActivity.access$super
09-30 22:03:37.354 3633-3633/ir.naserpour.ketabsara E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: ir.naserpour.ketabsara, PID: 3633
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.naserpour.ketabsara/ir.naserpour.ketabsara.MainActivity}: java.lang.NullPointerException
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2275)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2325)
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:141)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:136)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5352)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:515)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:863)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.NullPointerException
                                                                          at ir.naserpour.ketabsara.MainActivity.onCreate(MainActivity.java:89)
                                                                          at android.app.Activity.performCreate(Activity.java:5280)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2325) 
                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:136) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5352) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:863) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 
09-30 22:03:37.374 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:38.144 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:41.154 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:42.374 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:43.184 1326-1326/? E/StatusBar.BatteryController: BatteryController receive ACTION_BATTERY_CHANGED; Battery level = 100; Battery scale = 100
09-30 22:03:43.204 2024-2243/? E/HwSystemManager: BgPowerManagerService: conusmPower = 0 result = 0 flag1 =false flag2 = false
09-30 22:03:44.174 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:46.924 1177-1192/? E/WindowManager: Starting window AppWindowToken{4277aa00 token=Token{42292a40 ActivityRecord{41fbdea0 u0 ir.naserpour.ketabsara/.MainActivity t32}}} timed out
09-30 22:03:47.184 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:47.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:50.204 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:52.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:53.214 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:56.234 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:03:57.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:03:59.254 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:04:01.514 249-994/? E/CommandListener: readDefRoute call readDefRoute

                                                [ 09-30 22:04:01.514   249:  994 E/         ]
                                                isWifiDefaultRouteExisted /system/bin/ip route
09-30 22:04:02.264 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:04:02.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!

                                                        [ 09-30 22:04:02.514   249:  994 D/         ]
                                                        RouteDetect line= default via 192.168.1.1 dev wlan0 


                                                        [ 09-30 22:04:02.514   249:  994 D/         ]
                                                        ret is 0
09-30 22:04:02.514 249-994/? E/CommandListener: readDefRoute Rsp ok
09-30 22:04:02.514 1177-1177/? E/NetdConnector: NDC Command {219 route readDefRoute} took too long (1006ms)
09-30 22:04:05.284 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;
09-30 22:04:07.384 1458-1819/? E/PhoneInterfaceManager: sendRequest 16 timeout!
09-30 22:04:08.294 1716-1716/? E/wpa_supplicant: RSSI=-25;LINKSPEED=150;NOISE=9999;FREQUENCY=2462;

2 个答案:

答案 0 :(得分:0)

您收到NullPointerException,因为当您致电getInstance()时,mInstance为null

您是否在清单中注册了申请文件?

答案 1 :(得分:0)

您假设在调用单身人士之前将首先调用AppController。这是一种做单身人士的安全方式。

public class AppController extends Application {

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

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;


    public AppController(){
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

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

    public static synchronized AppController getInstance() {
        if(mInstance == null){
            mInstance = new AppController();
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        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);
        }
    }
}