- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在制作一个应用程序,该应用程序生成 JsonObjectRequest
并使用 Android 的 Volley 网络库从 URL 检索 JSON 数据。
AppController.java
public class AppController extends AppCompatActivity {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInstance = this;
}
public static synchronized AppController getInstance(){
return mInstance;
}
public RequestQueue getRequestQueue(){
if(mRequestQueue == null){
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
}
MainActivity.class 中的方法
private void makeJSONObjectRequest() {
showDialog();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
urlJsonObj, (String) null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
try {
//Parsing JSON Object response
String name = response.getString("name");
String email = response.getString("email");
JSONObject phone = response.getJSONObject("phone");
String home = phone.getString("home");
String mobile = phone.getString("mobile");
jsonResponse = "";
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Email: " + email + "\n\n";
jsonResponse += "Home: " + home + "\n\n";
jsonResponse += "Mobile: " + mobile + "\n\n";
txtResponse.setTag(jsonResponse);
}
catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
hideDialog();
}
},
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
VolleyLog.d(TAG+"Error:"+ volleyError.getMessage());
Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
});
/*THE ERROR OCCURS HERE! */
//adding request to the RequestQueue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
它给出了以下错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.h8pathak.volleyjson.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
我怎样才能纠正这个代码?
最佳答案
你的 AppController class
需要扩展 Application 类而不是 AppCompatActivity 类。
并记得更新您的 list 。 IE。使用 <application>
的名称属性在您的 AndroidManifest.xml 中添加此类标签。
<application
android:name=".AppController"/>
关于java - Volley AppController 类对象返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479808/
在阅读 cocoa 教程时,我注意到一些教程使用 AppDelegate 和一些 AppController 来定义打开各种窗口的 IBActions使用NSWindowController的子类。
如何在 app/app_controller.php 中调用 app_controller.php 中的函数在 app/plugins/media/models/behaviors/transfer.
在很多cakephp中的Acl组件的教程中我得到了在 AppController 或所有其他继承 AppController 的 Controller 中添加组件的指令...... 但问题是 var
我正在尝试按照本教程进行操作: https://angular.github.io/router/getting-started 为什么 AppController.$routeConfig 未定义?
我正在制作一个应用程序,该应用程序生成 JsonObjectRequest 并使用 Android 的 Volley 网络库从 URL 检索 JSON 数据。 AppController.java p
我刚改为使用 Typescript,但我一启动应用程序就收到错误消息。 这是我定义 AppController 的方式: app.controller('appController', AppCont
我正在使用 CakePHP 3。我一直在寻找使我的应用程序模块化的方法,有人建议我使用插件来做到这一点。所以我决定试一试。请看我之前的问题:Modular Directory Structure Wi
我正在尝试呈现错误模板(例如 error400.ctp),但默认布局(站点页眉和页脚)依赖于 AppController 中设置的组件和变量。我如何告诉 Cake 在呈现错误页面时使用 AppCont
我一直想知道为什么我的错误页面导致了我网站的某些页面不渲染,但后来我意识到这是因为 AppError extendsErrorHandler 而不是 AppController。这造成了一些变数我在
AppController 中未检测到 ajax 请求。 我已经打印了 params 数组。这是示例。 CakeRequest Object( [params] => Array
我将 CacheHelper 添加到我的应用程序中。在我的 APP/Config/core.php 中有 Configure::write('Cache.check', true); 在 APP/Co
我在导入 Firebase 时收到此错误“禁用模块时使用‘@import’”。 #import "GameKit/GKLocalPlayer.h" #import "GameKit/GKScore.h
我有一个名为AppController的类,我在 AppDelegate 中有以下方法,当选择菜单项时会调用该方法。 -(IBAction)selectSug:(id) sender { Ap
我使用的是 XCode 4,请注意,在设置新的 Cocoa 应用程序项目时,您会获得一个 AppDelegate.m 和 .h 文件,以及一个 .nib (.xib)。使用 alt-command-r
我正在尝试在 AppController 中对没有关联模型的表运行查询。我不想使用模型,因为这个查询会在每个请求上触发,我想使用模型会让它变慢一点。 我在一个论坛中发现,这可以通过 CakePHP 1
任何人都可以告诉我这个错误的来源是什么。我已经尝试了我在网上找到的所有解决方案,但都无济于事。我刚刚安装了一个 cakePHP 框架,但在 index.cpt 页面上出现了这个错误... 错误: fa
我目前正在为 iOS 编写 Cocos2d 游戏。 在这个游戏中,我创建了一个 GameState 单例以保存我的游戏状态(项目详细信息和位置、持续时间、分数等)。 我的主要 CCScene 包含一个
我正在为一个使用 CakePHP 2.3 的新站点编写单元测试。一切都很顺利,除了一件事。我想测试 beforeFilter() 和 beforeRender() 函数。 beforeFilter()
我想知道是否有可能在 AppController 和我的应用程序的其他 Controller 之间添加另一个抽象 Controller ? 这样我的 Controller ,例如UsersContro
我有一个功能,我希望我的所有 Controller 都能使用,所以我在 AppController 中定义了它。现在这个函数将做的部分事情与 Controller 无关,所以它应该在模型中,但由于这是
我是一名优秀的程序员,十分优秀!