我有一个应用程序,其中cookie被存储在WebView中。当我想获取Cookie时,在Android 4.0及更高版本中一切都很好,但是在Android Gingerbread中,我得到了NullPointerException。
我不知道该如何解决,有人可以帮忙吗?

public String getCookie(String siteName,String CookieName){
        String CookieValue = null;
        CookieSyncManager.createInstance(this.activity);
        CookieSyncManager.getInstance().startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        String cookies = cookieManager.getCookie(siteName);
        if(!cookies.equals(null)){
           String[] temp=cookies.split("[;]");
           for (String ar1 : temp ){
             if(ar1.contains(CookieName)){
                String[] temp1=ar1.split("[=]");
                CookieValue = temp1[1];
             }
           }
        }
        return CookieValue;
    }


这是LogCat,它指向if短语:

01-26 17:59:22.789: E/AndroidRuntime(28108):    FATAL EXCEPTION: main
01-26 17:59:22.789: E/AndroidRuntime(28108):    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.grevius.hhgvertretungsplan/de.grevius.hhgvertretungsplan.MainActivity}: java.lang.NullPointerException
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.os.Looper.loop(Looper.java:130)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread.main(ActivityThread.java:3835)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at java.lang.reflect.Method.invokeNative(Native Method)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at java.lang.reflect.Method.invoke(Method.java:507)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at dalvik.system.NativeStart.main(Native Method)
01-26 17:59:22.789: E/AndroidRuntime(28108):       Caused by: java.lang.NullPointerException
01-26 17:59:22.789: E/AndroidRuntime(28108):    at de.grevius.hhgvertretungsplan.MainActivity.getCookie(MainActivity.java:592)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at de.grevius.hhgvertretungsplan.MainActivity.onStart(MainActivity.java:560)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.Activity.performStart(Activity.java:3791)
01-26 17:59:22.789: E/AndroidRuntime(28108):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1731)
01-26 17:59:22.789: E/AndroidRuntime(28108):    ... 11 more


编辑:用Salmans代码,我收到此消息:

01-26 18:35:04.050: E/Main Activity(28872): Error occured while Extracting Cookie Value !, Error = java.lang.NullPointerException
01-26 18:35:04.050: W/System.err(28872):    at de.grevius.hhgvertretungsplan.MainActivity.getCookie(MainActivity.java:633)


它指的是if(!cookies.equals("")&&((!(CookieName!=null))&&(!CookieName.isEmpty()))){

最佳答案

我认为您传递的是主活动对象的无效引用。但是,崩溃可能有很多原因。 (例如无效的siteName / CookieName等)

这是修改代码的方法,以便它可以告诉您实际发生的情况。

    public String getCookie(String siteName,String CookieName){
    String CookieValue = "";
    String cookies = "";

    try
    {
        CookieSyncManager.createInstance(MainActivity.this);
        CookieSyncManager.getInstance().startSync();
        CookieManager cookieManager = CookieManager.getInstance();
        if(!siteName.isEmpty)
        {
            Log.d("Main Activity", "siteName = "+siteName);
            cookies = cookieManager.getCookie(siteName);
        }
        else
        {
            Log.w("Main Activity", "siteName is empty");
        }
    }
    catch(Exception e)
    {
        Log.e("Main Activity", "Error occured while getting Cookies !, Error = "+e.toString());
        e.printStackTrace();
    }

    try
    {
        if((!cookies.equals(""))&&((!CookieName==null)&&(!CookieName.isEmpty()))){
        String[] temp=cookies.split("[;]");
        for (String ar1 : temp ){
            if(ar1.contains(CookieName)){
                String[] temp1=ar1.split("[=]");
                CookieValue = temp1[1];
            }
        }
         else {
         Log.w("Main Activity", "cookies = "+cookies);
         Log.w("Main Activity", "CookieName= "+CookieName);
        }
       }
    catch(Exception e)
    {
        Log.e("Main Activity", "Error occured while Extracting Cookie Value !, Error = "+e.toString());
        e.printStackTrace();
    }
    return CookieValue;
}


我希望这有帮助。

10-07 13:21