package com.devinfotech.glind.activity;
import android.app.ProgressDialog;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.devinfotech.glind.R;
import com.devinfotech.glind.adapter.GalleryAdapter;
import com.devinfotech.glind.app.AppController;
import com.devinfotech.glind.model.Image;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private static final String endpoint = "http://api.androidhive.info/json/glide.json";
private ArrayList<Image> images;
private ProgressDialog pDialog;
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
pDialog = new ProgressDialog(this);
images = new ArrayList<>();
mAdapter = new GalleryAdapter(getApplicationContext(), images);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putSerializable("images", images);
bundle.putInt("position", position);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SlideshowDialogFragment newFragment = SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(ft, "slideshow");
}
@Override
public void onLongClick(View view, int position) {
}
}));
fetchImages();
}
private void fetchImages() {
pDialog.setMessage("Downloading json...");
pDialog.show();
JsonArrayRequest req = new JsonArrayRequest(endpoint,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
pDialog.hide();
images.clear();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
Image image = new Image();
image.setName(object.getString("name"));
JSONObject url = object.getJSONObject("url");
image.setSmall(url.getString("small"));
image.setMedium(url.getString("medium"));
image.setLarge(url.getString("large"));
image.setTimestamp(object.getString("timestamp"));
images.add(image);
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
});
// Adding request to request queue
AppController.getInstance()
.addToRequestQueue(req);
}
}
package com.devinfotech.glind.app;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by Lincoln on 04/04/16.
*/
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
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 <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);
}
}
}
错误:
10-10 13:01:02.030 23482-23482 /? E / dalvikvm:无法将符号链接/存储/内部添加到/ storage / emulated / legacy:文件存在 10-10 13:01:02.210 23482-23482 / com.devinfotech.glind E / dalvikvm:找不到类'android.graphics.drawable.RippleDrawable',从方法android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering中引用 10-10 13:01:02.300 23482-23482 /? E / AndroidRuntime:致命异常:主要 过程:com.devinfotech.glind,PID:23482 java.lang.RuntimeException:无法启动活动ComponentInfo {com.devinfotech.glind / com.devinfotech.glind.activity.MainActivity}:java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1243) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5372) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:970) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException 在com.devinfotech.glind.activity.MainActivity.fetchImages(MainActivity.java:123) 在com.devinfotech.glind.activity.MainActivity.onCreate(MainActivity.java:75) 在android.app.Activity.performCreate(Activity.java:5258) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 在android.app.ActivityThread.access $ 800(ActivityThread.java:151) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1243) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5372) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:970) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786) 在dalvik.system.NativeStart.main(本地方法)