我哪里错了?
我下载了一个项目,使用 JSON
库从 volley
显示列表视图。单独运行项目完全正常。然后我创建了一个导航抽屉,从中点击它我可以打开相同的列表视图。但是,当我尝试这样做时,我的应用程序停止提供以下 logcat
错误
10-27 03:06:18.072: E/MainActivity(1331): Error in creating fragment
10-27 03:06:18.072: I/Choreographer(1331): Skipped 64 frames! The application may be doing too much work on its main thread.
10-27 03:06:19.372: D/AndroidRuntime(1331): Shutting down VM
10-27 03:06:19.372: W/dalvikvm(1331): threadid=1: thread exiting with uncaught exception (group=0xb3aafba8)
10-27 03:06:19.502: E/AndroidRuntime(1331): FATAL EXCEPTION: main
10-27 03:06:19.502: E/AndroidRuntime(1331): Process: com.esggoa.iffi_app, PID: 1331
10-27 03:06:19.502: E/AndroidRuntime(1331): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esggoa.iffi_app/com.esggoa.iffi_app.MovieFragment}: java.lang.NullPointerException
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.os.Looper.loop(Looper.java:136)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-27 03:06:19.502: E/AndroidRuntime(1331): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 03:06:19.502: E/AndroidRuntime(1331): at java.lang.reflect.Method.invoke(Method.java:515)
10-27 03:06:19.502: E/AndroidRuntime(1331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-27 03:06:19.502: E/AndroidRuntime(1331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-27 03:06:19.502: E/AndroidRuntime(1331): at dalvik.system.NativeStart.main(Native Method)
10-27 03:06:19.502: E/AndroidRuntime(1331): Caused by: java.lang.NullPointerException
10-27 03:06:19.502: E/AndroidRuntime(1331): at com.esggoa.iffi_app.adapter.CustomListAdapter.<init>(CustomListAdapter.java:26)
10-27 03:06:19.502: E/AndroidRuntime(1331): at com.esggoa.iffi_app.MovieFragment.onCreate(MovieFragment.java:54)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.Activity.performCreate(Activity.java:5231)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-27 03:06:19.502: E/AndroidRuntime(1331): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-27 03:06:19.502: E/AndroidRuntime(1331): ... 11 more
提供错误的活动是 MovieActivity.java
public class MovieActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://api.androidhive.info/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_movie);
listView = (ListView) findViewById(R.id.list);
if(movieList != null) {
adapter = new CustomListAdapter(this, movieList);
}
else{
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
}
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// 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("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// 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);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的CustomListAdapter.java
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 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());
// 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;
}
}
这是AppController.java文件,其实例取空值但我没有得到如何解决它
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
答案 0 :(得分:1)
NPE:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
您的AppController.getInstance()
返回null
,原因是此处未见。
答案 1 :(得分:0)
将清单中的应用程序名称声明为appcontroller:
<application
android:name=".AppController"