我正在创建一个应用程序,在选项卡中有四个选项卡我正在使用json请求从url获取事件数据。当我运行代码时,它会显示空指针异常错误。
这是我的代码
package com.alex.jay.turnapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import static com.alex.jay.turnapp.AppController.*;
public class EventsActivity extends Fragment {
String url = "http://192.168.43.114/Turnapp/retrieveevents.php";
ProgressDialog dialog;
private List<EventItem> array_events = new ArrayList<>();
EventsAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View V = inflater.inflate(R.layout.events_tab, container, false);
RecyclerView recycler = (RecyclerView) V.findViewById(R.id.rv);
adapter = new EventsAdapter(getActivity(), array_events);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recycler.setLayoutManager(mLayoutManager);
recycler.setItemAnimator(new DefaultItemAnimator());
recycler.setAdapter(adapter);
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading events...");
dialog.show();
if (array_events != null) {
array_events.clear();
}
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
try {
hideDialog();
Log.d("Sever Message", s);
JSONObject events = new JSONObject(s);
JSONArray jsonArray = events.getJSONArray("events");
JSONObject jobject = null;
int len = jsonArray.length();
for (int i = 0; i < len; i++) {
jobject = jsonArray.getJSONObject(i);
EventItem eventsdata = new EventItem();
eventsdata.setEventname(jobject.getString("1"));
eventsdata.setVenue(jobject.getString("2"));
eventsdata.setImages(jobject.getString("3"));
eventsdata.setEventdate(jobject.getString("4"));
array_events.add(eventsdata);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
AppController.getInstance().addToRequestQueue(request);
recycler.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recycler, new ClickListener() {
@Override
public void onClick(View view, int position) {
Intent intent = new Intent(getActivity(), Events.class);
intent.putExtra("eventname", array_events.get(position).getEventname());
intent.putExtra("eventlocation", array_events.get(position).getvenue());
intent.putExtra("eventimage", array_events.get(position).getImages());
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
return V;
}
public void hideDialog() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
GestureDetector gestureDetector;
EventsActivity.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final EventsActivity.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recycler, MotionEvent e) {
View child = recycler.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, recycler.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView recycler, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
}
这是AppController代码
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 BitmapCache());
}
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);
}
}
}
这是错误
05-05 13:43:21.083 17540-17540/com.alex.jay.turnapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alex.jay.turnapp, PID: 17540
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.alex.jay.turnapp.AppController.addToRequestQueue(com.android.volley.Request)' on a null object reference
at com.alex.jay.turnapp.EventsActivity.onCreateView(EventsActivity.java:100)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:13716)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2815)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2822)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1560)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1301)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6607)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
请帮忙
答案 0 :(得分:0)
似乎方法AppController.getInstance()
返回NULL
。请查看如何实施以下Singleton
的示例。