- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个问题。我已经尝试让 http 请求工作很长一段时间了,并且我创建了一个新线程,以便该请求永远不会在主线程上运行。我已经成功地做到了这一点,以便发生 HTTP 请求,并且在请求线程中我已经做到了这一点,以便它记录从 http 请求中获取的数据。现在,如果我想在方法之外使用这个 jsonObject ,它会给我错误。帮忙?
日志猫:
02-23 22:48:08.612 4239-4239/com.tamrefrank.www.listviewandroidstudio E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tamrefrank.www.listviewandroidstudio, PID: 4239
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tamrefrank.www.listviewandroidstudio/com.tamrefrank.www.listviewandroidstudio.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONArray.toString()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONArray.toString()' on a null object reference
at com.tamrefrank.www.listviewandroidstudio.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-23 22:48:09.332 4239-4254/com.tamrefrank.www.listviewandroidstudio I/log_tag﹕ [{"Name":"Cabinet","Location":"In Storage","Latitude":"40.83990100","Longitude":"14.25185000","pk":19587,"image_id":"2006AJ6728"},{"Name":"Panel","Location":"Medieval and Renaissance, room 8, case WS EXP","Latitude":"48.87287900","Longitude":"4.63710000","pk":11371,"image_id":"2006AP3163"},{"Name":"Wallpaper","Location":"British Galleries, room 122f, case 1","Latitude":"53.47962200","Longitude":"-2.24881000","pk":19492,"image_id":"2006AU6265"},{"Name":"Portrait miniature","Location":"British Galleries, room 58c, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":9491,"image_id":"2006AH0271"},{"Name":"Caricature","Location":"In Storage","Latitude":"53.02513900","Longitude":"-2.18676000","pk":29979,"image_id":"2006AE9142"},{"Name":"Card game","Location":"In Storage","Latitude":"52.88328900","Longitude":"-1.97685000","pk":26483,"image_id":"2006AU5916"},{"Name":"Tile","Location":"Islamic Middle East, room 42, case WW9","Latitude":"39.76586200","Longitude":"64.42234000","pk":30220,"image_id":"2006AY5617"},{"Name":"Sculpture","Location":"Sculpture, room 22, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":36026,"image_id":"2006AM7780"},{"Name":"Oil painting","Location":"Prints & Drawings Study Room, room 315, case 24, shelf R","Latitude":"52.88328900","Longitude":"-1.97685000","pk":6905,"image_id":"2006AU6480"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10895,"image_id":"2006AU8783"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10902,"image_id":"2006AU9234"}]
02-23 22:48:09.332 4239-4254/com.tamrefrank.www.listviewandroidstudio I/log_tag﹕ [{"Name":"Cabinet","Location":"In Storage","Latitude":"40.83990100","Longitude":"14.25185000","pk":19587,"image_id":"2006AJ6728"},{"Name":"Panel","Location":"Medieval and Renaissance, room 8, case WS EXP","Latitude":"48.87287900","Longitude":"4.63710000","pk":11371,"image_id":"2006AP3163"},{"Name":"Wallpaper","Location":"British Galleries, room 122f, case 1","Latitude":"53.47962200","Longitude":"-2.24881000","pk":19492,"image_id":"2006AU6265"},{"Name":"Portrait miniature","Location":"British Galleries, room 58c, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":9491,"image_id":"2006AH0271"},{"Name":"Caricature","Location":"In Storage","Latitude":"53.02513900","Longitude":"-2.18676000","pk":29979,"image_id":"2006AE9142"},{"Name":"Card game","Location":"In Storage","Latitude":"52.88328900","Longitude":"-1.97685000","pk":26483,"image_id":"2006AU5916"},{"Name":"Tile","Location":"Islamic Middle East, room 42, case WW9","Latitude":"39.76586200","Longitude":"64.42234000","pk":30220,"image_id":"2006AY5617"},{"Name":"Sculpture","Location":"Sculpture, room 22, case 2","Latitude":"52.88328900","Longitude":"-1.97685000","pk":36026,"image_id":"2006AM7780"},{"Name":"Oil painting","Location":"Prints & Drawings Study Room, room 315, case 24, shelf R","Latitude":"52.88328900","Longitude":"-1.97685000","pk":6905,"image_id":"2006AU6480"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10895,"image_id":"2006AU8783"},{"Name":"Snuff bottle","Location":"In Storage","Latitude":"36.89445100","Longitude":"104.16564900","pk":10902,"image_id":"2006AU9234"}]
02-23 22:48:32.712 4239-4239/com.tamrefrank.www.listviewandroidstudio I/Process﹕ Sending signal. PID: 4239 SIG: 9
代码:
package com.tamrefrank.www.listviewandroidstudio;
//All the imports happen when you auto complete. Often you may have erros when you copy paste things directly because libraries may not get imported
import android.os.AsyncTask;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends ActionBarActivity {
private String [] drinks;
JSONArray jsonObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set the content view to activity_main.xml
setContentView(R.layout.activity_main);
JSONArray object = new JSONArray();
object = getData();
Log.i("log_tag", jsonObject.toString());
// System.out.print("success: " + object.toString());
drinks = new String[3];
drinks[0] = "hi";
drinks[1] = "5";
drinks[2] = "3";
/**
* Lists require adapters-Adapters are the place that we store the various data items we want
to appear in the Lists. This will be handled in an adapter.
*/
ArrayAdapter<String> adapter;
//We will create the array adapter and reference it to our individual List item from the ListView and then to the array we just created- Drinks.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,drinks);
//We finally will connect the adapter to the List View
ListView drinkLists = (ListView) findViewById(R.id.listView);
//Set the drinks array to the adapter below
drinkLists.setAdapter(adapter);
}
public JSONArray getData(){
new Thread(new Runnable() {
public void run() {
HttpGet httpGet = new HttpGet("https://stampwatch.herokuapp.com/");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
System.out.println(e);
}
try {
jsonObject = new JSONArray(stringBuilder.toString());
Log.i("log_tag", jsonObject.toString());
;
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("log_tag", jsonObject.toString());
}
}).start();
return jsonObject;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最佳答案
这里:
Log.i("log_tag", jsonObject.toString());
NPE
因为 jsonObject
为 null
并尝试使用 null 对象调用 JSONObject 的 toString()
方法。
But why is the object null if I have made a request with that object?
因为 jsonObject
对象是在 getData();
方法中初始化的,其中使用线程从 api 获取数据。意思是:
Log.i("log_tag", jsonObject.toString());
在启动线程后立即执行行,而不是等到线程完成。
关于java - HTTP 请求 Android - 尝试调用虚拟方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28689827/
有WHERE 1=1有什么作用如果您在脚本(伪代码)中编写此请求: sql = "SELECT f1,f2,f3 FROM t WHERE 1=1" ++ restOfTheClause
这个问题已经有答案了: R: Convert delimited string into variables (3 个回答) 已关闭 5 年前。 我有一个包含电影数据的表,在最后一列中,它包含电影所属
假设我有一个基类: struct A{ virtual void foo() = 0; }; 然后假设我有一个这样的派生类: struct B : public virtual A{ voi
我有一个小问题,我的 << 运算符没有被正确调用。 这是我的: class SomeInterface { friend std::ostream& operator<<(std::ostrea
首先,我来自 Java 社区,并且仍然是 C++ 的学习者。 请看下面的类 第二张图片显示了类“GameObject”的子类。它还有一个 Display() 方法。 GameObject类有5个子类,
我这里遇到了一些问题。我试图让我的代码像 java 中的接口(interface)一样工作。这个类被其他 2 个继承,因为它们导致了一些问题。而且我还想知道我是否做对了,以及改进我的代码的方法。我是新
在 C++ 中,我有一个基类 A,一个子类 B。两者都有虚方法 Visit。我想在 B 中重新定义“访问”,但 B 需要访问每个 A(以及所有子类)的“访问”功能。 我有类似的东西,但它告诉我 B 无
我有一个抽象类,它是类层次结构的根。该根类有一个带有一些简单实现的方法,似乎没有必要随时随地更改该实现。 使该方法成为非虚方法很好,但是某些子类可能会意外地重新实现它。在这种情况下,虚拟 final方
在 MSDN 上,我发现在抽象方法声明中使用“virtual”修饰符是错误的。我的一位同事应该是非常有经验的开发人员,但他在他的代码中使用了这个: public abstract class Busi
C++ 虚函数表是仅用于确定调用虚函数时应该执行哪一段代码,还是在运行时有其他用途? 在维基百科上,它列出了“动态调度”作为一个原因,但没有深入了解 C++ 的更多细节...... 最佳答案 一些实现
页面大小是否恒定?更具体地说,getconf PAGE_SIZE 给出 4096,这很公平。但这可以通过程序的运行时间改变吗?或者它在整个操作系统进程生成过程中是否保持不变。 IE。 , 进程是否可能
析构函数(当然还有构造函数)和其他成员函数之间的区别在于,如果常规成员函数在派生类中具有主体,则仅执行派生类中的版本。而在析构函数的情况下,派生版本和基类版本都会被执行? 很高兴知道在析构函数(可能是
如果一个函数被定义为虚函数并且与纯虚函数相同,这究竟意味着什么? 最佳答案 来自 Wikipedia's Virtual function... In object-oriented programm
我有一个在 Jetty 下运行的应用程序,我希望该应用程序返回自引用绝对 URL(生成 RSS 提要时,因此客户端必须能够在没有“当前 URL”上下文的情况下工作)。 问题是我事先不知道应用程序将部署
如何在两个virtualtreeview之间复制以复制所有列,而不仅仅是第一列? 复制前: 复制后: 最佳答案 树控件不保存任何数据。它不包含要显示的列数据,因此无法复制它。而是,当树控件想要显示任何
我已将 ShowHint 设置为 true 并将 HintMode 设置为 hmToolTip,但是当我将光标悬停在控件上时,我的 OnGetHint() 事件处理程序甚至没有断点。 知道我做错了什么
我的 friend 正在 Delphi 中使用 VirtualTreeView 工作,并且遇到了下一个问题:他有两列,第一列的每一行都有数据和子项。是否可以不更改第一列宽度来设置最大子列宽度? 图例:
我在我的 Virtual TreeView Component 中使用 TVirtualStringTree ( Delphi project 的一部分)我想创建一个 View ,其中 2 列可以有可
我想遍历 VirtualTreeView 的所有根并将其删除。 我不想清除它。 我收到此代码的访问冲突: var Node : PVirtualNode; begin if VirtualStri
我有一个可以输出表单的 PHP 文件。我想在服务器端调用这个 PHP 文件(当前使用“include”),填写并提交。 这样更好,因此我不必干预实际的 PHP 表单,只需处理表示层,以便数据可以被它自
我是一名优秀的程序员,十分优秀!