在logcat中,我收到以下信息: “java.lang.NullPointerException:尝试调用接口方法'int
java.util.List.size()' on a null object reference"
package com.example.kuba.seriale;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import com.example.kuba.seriale.models.MovieModel;
import java.util.List;
public class MainActivity extends AppCompatActivity {
SearchView src;
Button btn;
private TextView txv;
ListView lvm;
String api_key = "***************";
JSONTasks json = new JSONTasks();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
src = (SearchView) findViewById(R.id.searchView);
txv = (TextView) findViewById(R.id.textView);
btn = (Button) findViewById(R.id.button);
lvm = (ListView)findViewById(R.id.lvMovies);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String zapytanie = src.getQuery().toString();
if((zapytanie==null)||(zapytanie=="")){
zapytanie = "Homeland";
}
String Link = "http://api.themoviedb.org/3/search/tv?query="+zapytanie+"&api_key="+api_key;
MovieAdapter movieAdapter = new MovieAdapter(getApplicationContext(), R.layout.row, json.mvc(Link));
try{
lvm.setAdapter(movieAdapter);
} catch (Exception e){
txv.setText(e.getMessage());
}
}
});
src.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
}
public void Search(){
String zapytanie = src.getQuery().toString();
if((zapytanie==null)||(zapytanie=="")){
zapytanie = "Homeland";
}
String Link = "http://api.themoviedb.org/3/search/tv?query="+zapytanie+"&api_key="+api_key;
MovieAdapter movieAdapter = new MovieAdapter(getApplicationContext(), R.layout.row, json.mvc(Link));
lvm.setAdapter(movieAdapter);
}
}
class MovieAdapter extends ArrayAdapter{
private List<MovieModel> movieModelList;
private int resource;
private LayoutInflater inflater;
LinearLayout linear;
Button btn;
public MovieAdapter(Context context, int resource, List<MovieModel> objects) {
super(context, resource, objects);
movieModelList = objects;
this.resource = resource;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = inflater.inflate(R.layout.row,null);
}
TextView txv;
txv = (TextView)convertView.findViewById(R.id.textView2);
btn = (Button)convertView.findViewById(R.id.button2);
linear = (LinearLayout)convertView.findViewById(R.id.linear);
txv.setText(movieModelList.get(position).getName());
int id = movieModelList.get(position).getId();
return convertView;
}
public void click(String id){
btn.setText(id);
}
}