空对象引用上的 NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)'
NullPointerException addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
我正在使用 AndroidHive register login,它在这个登录注册的示例项目中运行良好。
但是在 CardView
s 和其他小部件尝试了很多次之后,这个错误出现在 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.access0(RegisterActivity.java:35)
at client.myproject.RegisterActivity.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 库。
在你的AndroidManifest.xml
中添加
<application android:name="YOURPACKAGENAME.AppController"
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
正如 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">
在我的例子中,我忘记了初始化变量 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);
你没有传递任何数据给 volley 方法,这意味着它得到的是空数据(空数据)......
参见示例:
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map=new HashMap<>();
map.put(region, regionName);
return map;
}
如果 regionName 为空,它会给你 NullPointerException,所以 regionName 必须有一些东西.....
在清单文件中添加 appcontroller,如图所示
<application android:name="app.AppController"
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
请检查您是否已将 requestQueue 对象初始化为:
requestQueue = Volley.newRequestQueue(this);
在 onCreate() 中试试这个
requestQueue=Volley.newRequestQueue(getApplicationContext());
我有同样的错误,因为我在onCreate
方法
中错误地引用了错误的xml文件
// R.layout.activity_calender 应该与您的 activity (CalenderActivity)
的 xml 文件相同
setContentView(R.layout.activity_calender);
我正在使用 AndroidHive register login,它在这个登录注册的示例项目中运行良好。
但是在 CardView
s 和其他小部件尝试了很多次之后,这个错误出现在 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.access0(RegisterActivity.java:35)
at client.myproject.RegisterActivity.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 库。
在你的AndroidManifest.xml
中添加
<application android:name="YOURPACKAGENAME.AppController"
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
正如 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">
在我的例子中,我忘记了初始化变量 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);
你没有传递任何数据给 volley 方法,这意味着它得到的是空数据(空数据)...... 参见示例:
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map=new HashMap<>();
map.put(region, regionName);
return map;
}
如果 regionName 为空,它会给你 NullPointerException,所以 regionName 必须有一些东西.....
在清单文件中添加 appcontroller,如图所示
<application android:name="app.AppController"
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
请检查您是否已将 requestQueue 对象初始化为:
requestQueue = Volley.newRequestQueue(this);
在 onCreate() 中试试这个
requestQueue=Volley.newRequestQueue(getApplicationContext());
我有同样的错误,因为我在onCreate
方法
// R.layout.activity_calender 应该与您的 activity (CalenderActivity)
的 xml 文件相同
setContentView(R.layout.activity_calender);