我想在后台加载来自VK API的一些数据,所以要做到这一点,我创建自己的服务" VKNotifications"并设置Thread getNews:
Thread getNews = new Thread(new Runnable() {
public void run() {
Log.d("BTCR", "Starting Thread getNews");
while (true) {
Log.d("BTCR", "THREAD getNews");
try {
sleep(2000);
vkNews.get(1, 8);
Log.d("BTCR", "Getting news from source");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
我创建vkNews对象并从服务onCreate启动线程:
@Override
public void onCreate() {
super.onCreate();
Log.d("BTCR", "Start service VKNotifications");
vkNews = new VKNews(this);
getNews.start();
}
然后我从AuthorizedActivity onCreate开始服务它:
startService(new Intent(AuthorizedActivity.this, VKNotifications.class));
并收到此错误:
07-09 02:36:03.651 14239-14257/id.mirgorod.btcrussia:VKNotifications E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-217
Process: id.mirgorod.btcrussia:VKNotifications, PID: 14239
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.vk.sdk.api.VKRequest.getLang(VKRequest.java:550)
at com.vk.sdk.api.VKRequest.getPreparedParameters(VKRequest.java:274)
at com.vk.sdk.api.httpClient.VKHttpClient.requestWithVkRequest(VKHttpClient.java:127)
at com.vk.sdk.api.VKRequest.getPreparedRequest(VKRequest.java:297)
at com.vk.sdk.api.VKRequest.getOperation(VKRequest.java:315)
at com.vk.sdk.api.VKRequest.start(VKRequest.java:376)
at com.vk.sdk.api.VKRequest.executeWithListener(VKRequest.java:234)
at id.mirgorod.btcrussia.VKNews.get(VKNews.java:46)
at id.mirgorod.btcrussia.VKNotifications$1.run(VKNotifications.java:36)
at java.lang.Thread.run(Thread.java:818)
07-09 02:36:03.667 14239-14255/id.mirgorod.btcrussia:VKNotifications D/
这是我的VKNews对象代码,方法得到:
public void get(int offset, int count){
final JSONArray returnArray = new JSONArray();
VKRequest request = VKApi.wall().get(VKParameters.from("domain", "bitcoin", VKApiConst.OFFSET, offset, VKApiConst.COUNT, count));
request.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onError(VKError error) {
System.out.println("error: " + error);
}
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
try {
json = response.json.getJSONObject("response").getJSONArray("items");
for (int i = 0; i < json.length(); i++) {
returnArray.put(json.getJSONObject(i).get("id").toString());
}
gettedID = returnArray;
Log.d("BTCR", "gettedID: " + returnArray.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
//I don't really believe in progress
}
});
}
如果我从AuthorizedActivity运行Thread getNews,它的工作完美,但如果我从服务运行它,我会收到错误。我做错了什么?
答案 0 :(得分:1)
您需要设置应用程序上下文。 API似乎始终使用上下文。
<audio src="http://www.noiseaddicts.com/samples_1w72b820/1456.mp3" autoplay loop>
Your browser does not support the <code>audio</code> element.
</audio>
<audio src="http://www.noiseaddicts.com/samples_1w72b820/3717.mp3" autoplay loop>
Your browser does not support the <code>audio</code> element.
</audio>
<audio src="http://www.noiseaddicts.com/samples_1w72b820/118.mp3" autoplay loop>
Your browser does not support the <code>audio</code> element.
</audio>
<input id="stopButton" type="button" value="Stop All Audio" />