我正在编写这个从rottentomatoes API获取电影信息的应用程序。我使用客户端进行解析和httprequests。
首先我尝试设置一个searchview,但由于我使用了framents(FragmentNavigationDrawer),所以它不是一个选项。
下一个最好的事情。带有ListPopupWindow的EditText。
我设置了textchangelistener,这样当我开始在给定的edittext(搜索)中输入东西时,它应该执行asynctask(使用edittext输入作为api请求的搜索词)。然而,没有任何反应。不是一件事。在logcat中有一行红色文本,应用程序不会崩溃,没有任何东西。所以我在stackoverflow转向你们神级编码员。
片段:
public class SecondFragment extends Fragment{
ListPopupWindow listPopupWindow;
EditText search;
List<Movie> result = new ArrayList<Movie>();
private MovieAdapter adpt;
MovieSearch searchTask = new MovieSearch();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.secondfragment, container, false);
search = (EditText) view.findViewById(R.id.searchBox2);
listPopupWindow = new ListPopupWindow(getActivity());
adpt = new MovieAdapter(new ArrayList<Movie>(), getActivity());
searchTask = new MovieSearch();
listPopupWindow.setAdapter(adpt);
listPopupWindow.setAnchorView(search);
listPopupWindow.setWidth(300);
listPopupWindow.setHeight(400);
listPopupWindow.setModal(true);
search.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3){
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3){
}
@Override
public void afterTextChanged(Editable editable){
searchTask.cancel(true);
String input = search.getText().toString();
searchTask.execute(input);
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.secondfragment, container, false);
}
@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);
}
public void onClick(View view) {
// detect the view that was "clicked"
switch (view.getId()) {
case R.id.addToCollection: //just a placeholder
break;
}
}
private class MovieSearch extends AsyncTask<String, Void, List<Movie>> {
@Override
protected void onPostExecute(List<Movie> result) {
super.onPostExecute(result);
adpt.setMovieList(result);
adpt.notifyDataSetChanged();
}
@Override
protected List<Movie> doInBackground(String... params){
Log.e("Status", "Entered thread");
String input = (params[0]);
JTomato jtomato = new JTomato("jf6cykts4sybqrex3dkjd7uq");
jtomato.setPage_limit(1);
jtomato.searchMovie(input, result, 0);
if (isCancelled())
{
//Notify your activity that you had canceled the task
return (null);
}
return result;
}
}
}
答案 0 :(得分:1)
你永远不会创建和AsyncTask MovieSearch searchTask的实例,它始终为null。
添加此代码
searchTask = new MovieSearch();
之前打电话
searchTask.execute()
;