- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试从 AsyncTask
中获取结果以显示在我的搜索 Activity 中。
我有一个来自以前应用程序的示例,它是在一位同事的帮助下制作的(我是那里的实习生)。
我根据这个应用程序的需要调整了示例,但现在我得到了 NullPointerException
:
03-04 03:50:23.865: E/AndroidRuntime(8224): FATAL EXCEPTION: main
03-04 03:50:23.865: E/AndroidRuntime(8224): Process: com.cyberdog.what2watch, PID: 8224
03-04 03:50:23.865: E/AndroidRuntime(8224): java.lang.NullPointerException: Attempt to invoke interface method 'void com.cyberdog.what2watch.JsonHandling$IOnFinish.onGetData(org.json.JSONObject)' on a null object reference
03-04 03:50:23.865: E/AndroidRuntime(8224): at com.cyberdog.what2watch.JsonHandlingTMDBAsync.onPostExecute(JsonHandling.java:321)
03-04 03:50:23.865: E/AndroidRuntime(8224): at com.cyberdog.what2watch.JsonHandlingTMDBAsync.onPostExecute(JsonHandling.java:1)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.os.Looper.loop(Looper.java:135)
03-04 03:50:23.865: E/AndroidRuntime(8224): at android.app.ActivityThread.main(ActivityThread.java:5274)
03-04 03:50:23.865: E/AndroidRuntime(8224): at java.lang.reflect.Method.invoke(Native Method)
03-04 03:50:23.865: E/AndroidRuntime(8224): at java.lang.reflect.Method.invoke(Method.java:372)
03-04 03:50:23.865: E/AndroidRuntime(8224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
03-04 03:50:23.865: E/AndroidRuntime(8224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
我还检查了关于 onPostExecute()
的其他示例,看来我做对了......但仍然出现错误。
我在 onPostExecute()
处使用了一个断点,结果不为空,尽管它确实运行了两次代码。
异步类的代码:
class JsonHandlingTMDBAsync extends AsyncTask<String, Void, JSONObject> {// params,
// progress,
// result
private String api_key_tmdb = "2fb98d2bca5895c89a6efaf70903f706";
private String tmdb_multi_url = "http://api.themoviedb.org/3/search/multi?query=";
int Total_pages;
int number_entries_per_page;
private JSONObject resultObject;
private IOnFinish listener;
String Titel="";
String Year ="";
public JsonHandlingTMDBAsync(String titel, String year, IOnFinish listener) {
this.listener = listener;
this.Titel=titel;
this.Year=year;
}
@Override
protected JSONObject doInBackground(String... params) {
return loadJSON(params);
}
private JSONObject loadJSON(String... params) {
resultObject = new JSONObject();
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet;
if (Titel.trim().length() == 0 && Year.trim().length() != 0) {
httpGet = new HttpGet(tmdb_multi_url + Year + "&api_key="
+ api_key_tmdb);
} else if (Titel.trim().length() != 0 && Year.trim().length() == 0) {
httpGet = new HttpGet(tmdb_multi_url + Titel + "&api_key="
+ api_key_tmdb);
} else if (Titel.trim().length() == 0 && Year.trim().length() == 0) {
System.out.print("error null titel/year");
httpGet = new HttpGet(tmdb_multi_url + "star+wars&api_key="
+ api_key_tmdb);
} else {
httpGet = new HttpGet(
"http://www.omdbapi.com/?s=star&y=2008&r=JSON"
+ "&plot=short");
}
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(JsonHandling.class.toString(), "Failed to download file");
}
resultObject = new JSONObject(builder.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultObject;
}
@Override
protected void onPostExecute(JSONObject result){
super.onPostExecute(result);
listener.onGetData(result);
}}
Activity 代码:
package com.cyberdog.what2watch;
import java.util.List;
import org.json.JSONObject;
import com.cyberdog.what2watch.JsonHandling.IOnFinish;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class SearchActivity extends Activity implements OnScrollListener, IOnFinish{
/** Called when the activity is first created. */
private static SearchCustomAdapter adapter;
EditText tvTitel = null;
EditText tvYear = null;
ListView lvSearch = null;
JsonHandling jh = new JsonHandling(this);
int pageNumber =0;
int previeusTotal=0;
private static List<Serie> searchResult;
private IOnFinish iof;
private static List<Serie>series;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
tvTitel = (EditText) findViewById(R.id.etf_search_titel);
tvYear = (EditText) findViewById(R.id.etf_search_year);
lvSearch = (ListView)findViewById(R.id.lvSearch);
Button btnSearch = (Button) findViewById(R.id.btnSearch_search);
btnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
JsonHandling.getInstance().getJSONFromUrl(tvTitel.getText().toString(), tvYear.getText().toString(),iof);
}
});
}
public void search(int pageNumber){
// adapter for the custom list
//lvSearch = (ListView) findViewById(R.id.lvSearch);
// SearchCustomAdapter lvAdapt = new SearchCustomAdapter(
// SearchActivity.this, jh.search_db(tvTitel.getText()
// .toString(), tvYear.getText().toString(),
// searchSite, pageNumber));
// lvSearch.setAdapter(lvAdapt);
//lvAdapt.notifyDataSetChanged();
}
@Override
public void onGetData(JSONObject obj) {
if (obj == null) {
Toast.makeText(this,
"obj is null",
Toast.LENGTH_LONG).show();
Serie serie = new Serie("not found","");
Serie[] seriesList = new Serie[series.size()];
seriesList[0] = serie;
adapter = new SearchCustomAdapter(this, seriesList);
lvSearch = (ListView) findViewById(R.id.lvSearch);
lvSearch.setAdapter(adapter);
}
if(obj !=null){
if (JsonHandling.pageNumber == 0){
series =Serie.serieListFromJSON(this, JsonHandling.getInstance().getJSONArraySerieFromUrlTMDB(obj));
Serie[] seriesList =series.toArray(new Serie[series.size()]);
adapter = new SearchCustomAdapter(this, seriesList);
lvSearch = (ListView)findViewById(R.id.lvSearch);
lvSearch.setAdapter(adapter);
//lvSearch.setOnItemClickListener(this);
lvSearch.setOnScrollListener(this);
}}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem+visibleItemCount;
totalItemCount = 20+20 * pageNumber;
//int totalSearchResults = 0;
Log.i("total, last in screen, pagenumber outside", totalItemCount+":" + lastInScreen+ ":"+pageNumber);
if((lastInScreen +20*pageNumber == totalItemCount)){
Log.i("total, last in screen, pagenumber inside", totalItemCount+":" + lastInScreen+ ":"+pageNumber);
pageNumber++;
search(pageNumber);
}
else{
}
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
}
}
和 json/interface 部分代码:
public interface IOnFinish{
void onGetData(JSONObject obj);
}
public void getJSONFromUrl(String titel, String year, IOnFinish listener) {
JsonHandlingTMDBAsync task = new JsonHandlingTMDBAsync(titel, year, listener);
task.execute();
}
在我的 Activity 类中,我在 onGetData()
上有一个断点,但它从未到达,尽管 onPostExecute()
有对象并被调用了两次(不知道为什么)。
最佳答案
Attempt to invoke interface method 'void com.cyberdog.what2watch.JsonHandling$IOnFinish.onGetData
因为IOnFinish
接口(interface)的iof
对象是null
。
在 setContentView
之后初始化 iof
:
setContentView(R.layout.search);
iof=this;
关于android - java.lang.NullPointerException : Attempt to invoke interface method on a null object reference error on OnPostExecute() - AsyncTask 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28845954/
我有以下代码: Public Delegate Sub SetStatusBarTextDelegate(ByVal StatusText As String) Private Sub SetStat
在调用 Invoke-RestMethod 时使用 Powershell,例如: Invoke-RestMethod -Method Get -Uri "https://google.com/api/
我正在尝试将 Winform 应用程序转换为控制台应用程序。 Winform 应用程序有一个委托(delegate)处理程序。如何在 console 应用程序中编写相同的功能? this.Invoke
在 WPF 中,Dispatcher.Invoke 和直接在控件实例上调用的 Invoke 有什么区别。据我了解,调度程序负责处理线程的消息,Control.Invoke 是否会继续调用 Dispat
我正在研究性能监控系统,它可以将其例程注入(inject)现有程序集。为此,我试图了解 dalvik 代码的工作原理。 下面是我要完成的工作的示例。输入类可能如下所示: class MyClass{
我正在使用 powershell 命令来执行脚本和 cmdlet。因此,在执行 cmdlet 时,我使用了 powershell.invoke,而在执行脚本时,我使用了 pipeline.invoke
有人能解释一下 Invoke-Expression $test 之间的区别吗?和 Invoke-Expression -Command $test ? 变量测试是: $test = "notepad
我有四个类,即 MapperOne、ReducerOne、MapperTwo、ReducerTwo。我想要其中的一个链。 MapperOne-->ReducerOne-->输出文件生成,输入到Mapp
我正在阅读 Java ForkJoin 框架。不直接在 ForkJoinTask 的实现上调用 invoke()(例如 RecursiveTask),而是实例化 ForkJoinPool 有什么额外的
我在调用 Invoke-SqlCmd 时遇到问题,因为它包含第二个 Invoke-SqlCmd 调用: function Get-Foo { $query=` @" WITH data AS (
有人知道如何解决这个问题吗?我创建了一个客户端来使用网络服务。客户端代码为: package cliente; import java.util.List; import handler.Header
我希望使用 P/Invoke 来允许我的 C# 程序集与 native C 库互操作;这需要是跨平台的(即 Mono),因此不能使用混合模式程序集。我想知道使用不安全的 P/invoke 调用并在不安
一般来说,我对使用 Invoke-RestMethod/Invoke-WebRequest 比较陌生 - 我认为这是以下问题的重要背景。 我正在调用如下电话: $Headers = @{ "A
在 Jenkins 的一个自由风格项目(不是说 Maven2/3 项目)中,我有两个可能的构建步骤: 调用 Maven 3 调用顶级 Maven 目标 在不同的安装中,我有不同的组合(有些两者都有,有
这是完整的错误: e: C:\Users\HP\AndroidStudioProjects\MoneyManager\app\src\main\java\com\cruxrepublic\moneym
我正在编写 jQuery 插件并将它们与 AJAX 集成。我正在减少脂肪并专注于基础知识: (function($) { function MyPlugin(el, options) {
有人可以建议我如何处理这条消息吗? CA1060 Move P/Invokes to NativeMethods class Because it is a P/Invoke method, 'UCo
在java中我们可以“用类名调用一个静态方法”也可以“用一个对象调用一个静态方法”java中“用类名调用静态方法”和“用对象调用静态方法”有什么区别? 最佳答案 没有区别,但建议以静态方式调用 sta
尝试从对话框中的 EditText 获取 Edit Text 的值,但一次又一次地出现此错误 Attempt to invoke virtual method 'android.text.Editab
我正在开发一款扑翼应用程序。在出现此错误之前,读取和写入FireStore数据库没有任何问题,但随后突然出现错误(如下所示),并阻止我读取或写入数据库。我一直在寻找答案,但不幸的是,我找不到任何可以解
我是一名优秀的程序员,十分优秀!