当我尝试运行应用程序时没有错误日志,但当我打开MovieFragment我的应用程序不幸关闭idk是什么问题,但我认为是我的适配器的问题,请有人帮助我
btw即时通讯新手使用片段bc更容易
MovieFragment.java
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MovieFragment extends Fragment{
// Log tag
private static final String TAG = MovieFragment.class.getSimpleName();
// Movies json url
private static final String url = "http://...../json.php";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
public MovieFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
// Inflate the layout for this fragment
final ListView listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("name"));
movie.setThumbnailUrl(obj.getString("images1"));
//movie.setDescribe(obj.getString("describe"));
//movie.setRating(((Number) obj.get("rating"))
// .doubleValue());
movie.setYear(obj.getInt("id"));
movie.setTipe(obj.getString("tipe"));
/*// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);*/
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
CustomeListAdapter.java
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.List;
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView describe = (TextView) convertView.findViewById(R.id.describe);
TextView tipe = (TextView) convertView.findViewById(R.id.tipe);
// TextView rating = (TextView) convertView.findViewById(R.id.rating);
// TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
Movie m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
//describe
// describe.setText(m.getDescribe());
//tipe
tipe.setText(m.getTipe());
// rating
//rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
// String genreStr = "";
// for (String str : m.getGenre()) {
// genreStr += str + ", ";
//}
//genreStr = genreStr.length() > 0 ? genreStr.substring(0,
// genreStr.length() - 2) : genreStr;
// genre.setText(genreStr);
// release year
year.setText(String.valueOf(m.getYear()));
return convertView;
}
}
记录错误
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: FATAL EXCEPTION: main
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: Process: com.felix.tgpfg, PID: 17482
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: java.lang.NullPointerException
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at com.felix.tgpfg.MovieFragment.onCreateView(MovieFragment.java:71)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 15:40:26.543 17482-17482/com.felix.tgpfg E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)