【问题标题】:NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object referenceNullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' 在空对象引用上
【发布时间】:2015-07-08 19:32:12
【问题描述】:

我正在使用AndroidHive register login,它在此登录注册的示例项目中运行良好。

但是在多次尝试使用CardViews 和其他小部件后,LogCat 上出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void client.myproject.app.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
            at client.myproject.RegisterActivity.registerUser(RegisterActivity.java:185)
            at client.myproject.RegisterActivity.access$300(RegisterActivity.java:35)
            at client.myproject.RegisterActivity$1.onClick(RegisterActivity.java:81)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

虽然这些代码在单个应用程序中运行良好(只需注册登录)。

我正在使用 Volley 库。

【问题讨论】:

  • 您是否将 AppController 添加到 AndroidManifest.xml 中?

标签: android android-volley


【解决方案1】:

在你的AndroidManifest.xml 添加

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

【讨论】:

  • 这是因为AppController,它扩展了Application
  • 在这里您可以阅读Application 的用途:developer.android.com/reference/android/app/Application.html
  • 我也面临同样的问题。但我正在应用程序中间进行登录和注册过程。所以我不能将它添加到应用程序标签中。我尝试将它添加到活动标签,但仍然是同样的错误。有什么帮助吗?
  • 什么是老板。工作。
【解决方案2】:

正如 N1to 所说,您需要在 AndroidManifest.xml 中添加控制器,如果您不添加它,则永远不会调用 onCreate(),并且当您调用 AppController.getInstance() 时,实例为空。

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

它也适用于我:

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

【讨论】:

    【解决方案3】:

    在我的例子中,我忘记初始化变量 rq,请确保你做到了

        ...
        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);
    

    【讨论】:

      【解决方案4】:

      在清单文件中添加应用控制器,如图所示

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

      【讨论】:

        【解决方案5】:

        请检查您是否已将 requestQueue 对象初始化为:

        requestQueue = Volley.newRequestQueue(this);
        
        

        【讨论】:

          【解决方案6】:

          您没有将任何数据传递给 volley 方法,这意味着它获得了空数据(空数据)..... 见例子:

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

          如果 regionName 为空,它会给你 NullPointerException,所以 regionName 必须有一些东西.....

          【讨论】:

            【解决方案7】:

            在 onCreate() 里面试试这个

            requestQueue=Volley.newRequestQueue(getApplicationContext());
            

            【讨论】:

              【解决方案8】:

              我有同样的错误,因为我在onCreate方法中错误地引用了错误的xml文件

              // R.layout.activity_calender 应该是你的activity(CalenderActivity)的同一个xml文件
              setContentView(R.layout.activity_calender);

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-10-29
                • 2016-07-29
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多