NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference

后端 未结 7 1543
你的背包
你的背包 2020-12-09 08:32

I\'m using AndroidHive register login and it\'s working fine in example project of this login-register.

But after many attempts trying it with CardViews

相关标签:
7条回答
  • 2020-12-09 09:15

    In your AndroidManifest.xml add

    <application android:name="YOURPACKAGENAME.AppController" 
                 android:allowbackup="true" 
                 android:icon="@drawable/ic_launcher" 
                 android:label="@string/app_name"
                 android:theme="@style/AppTheme">
    
    0 讨论(0)
  • 2020-12-09 09:18

    try this inside onCreate()

    requestQueue=Volley.newRequestQueue(getApplicationContext());
    
    0 讨论(0)
  • 2020-12-09 09:21

    In menifest file add appcontroller as shown

    <application android:name="app.AppController" 
             android:allowbackup="true" 
             android:icon="@drawable/ic_launcher" 
             android:label="@string/app_name"
             android:theme="@style/AppTheme">
    
    0 讨论(0)
  • 2020-12-09 09:22

    In my case, I forgot to initialize the variable rq, please, make sure you did it

        ...
        private RequestQueue rq;   // rq = null (NullPointerException if you use it)
        ...
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            rq = Volley.newRequestQueue(YourActivity.this);  // rq != null
        }
        ...
        rq.add(request);
    
    0 讨论(0)
  • 2020-12-09 09:27

    You didn't pass any data to the volley method, that means it get null data (empty data)..... see example:

    protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> map=new HashMap<>();
                    map.put(region, regionName);
                    return map;
                }
    

    if regionName is empty it will give you NullPointerException, so regionName must have something.....

    0 讨论(0)
  • 2020-12-09 09:32

    As N1to says, you need to add your controller in the AndroidManifest.xml, if you don't add it then the onCreate() is never called and when you call AppController.getInstance() the instance is null.

    <application android:name="YOURPACKAGENAME.AppController" 
             android:allowbackup="true" 
             android:icon="@drawable/ic_launcher" 
             android:label="@string/app_name"
             android:theme="@style/AppTheme">
    

    It also works for me with:

    <application android:name=".AppController" 
             android:allowbackup="true" 
             android:icon="@drawable/ic_launcher" 
             android:label="@string/app_name"
             android:theme="@style/AppTheme">
    
    0 讨论(0)
提交回复
热议问题