NullPointerException:空对象引用, addToRequestQueue(com.android.volley.Request,java.lang.String)在一个空对象引用上。

25

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

但是,在尝试将其与CardView和其他小部件一起使用多次后,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库。


2
你是否将AppController添加到AndroidManifest.xml文件中了? - isma3l
8个回答

78

在你的AndroidManifest.xml中添加

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

2
这是因为AppController继承了Application - Damian Kozlak
2
在这里,您可以了解Application的用途:http://developer.android.com/reference/android/app/Application.html - Damian Kozlak
我也遇到了同样的问题。但是我正在应用程序的中间制作登录和注册过程。所以我不能将它添加到应用程序标签中。我尝试将其添加到活动标签中,但仍然出现相同的错误。有什么帮助吗? - Lokesh Pandey
干得好!工作完成。 - Mohammad Eskandari

7

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

<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">

6

在我的情况下,我忘记初始化变量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);

3
在清单文件中添加appcontroller,如下所示。
<application android:name="app.AppController" 
         android:allowbackup="true" 
         android:icon="@drawable/ic_launcher" 
         android:label="@string/app_name"
         android:theme="@style/AppTheme">

2
请检查是否已经初始化了您的requestQueue对象,如下所示:

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

requestQueue = Volley.newRequestQueue(this);


0

你没有向Volley方法传递任何数据,这意味着它获取到了空数据(即空数据)......请参考以下示例:

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

如果regionName为空,它会给你一个NullPointerException,所以regionName必须有一些内容.....

0

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

// R.layout.activity_calender 应该是你的活动(CalenderActivity)所使用的相同的xml文件
setContentView(R.layout.activity_calender);


0

在 onCreate() 方法中尝试这个

requestQueue=Volley.newRequestQueue(getApplicationContext());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接