(澳门博彩导航网址) Android使用volley的一个空指针问题
澳门赌场开户
澳门赌场开户
当前位置 : 澳门赌场开户 > 澳门博彩导航网址

(澳门博彩导航网址) Android使用volley的一个空指针问题

使用了volley的listview,但是直接停止运行。
小弟看了一天,还是不明白,问题出在哪里,求指点
错误提示:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: com.xd.v2ex, PID: 32632
                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xd.v2ex/com.xd.v2ex.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method void com.xd.v2ex.utils.ApplicationController.addToRequestQueuecom.android.volley.Request on a null object reference
                                                             at android.app.ActivityThread.performLaunchActivityActivityThread.java:2702
                                                             at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2767
                                                             at android.app.ActivityThread.access$900ActivityThread.java:177
                                                             at android.app.ActivityThread$H.handleMessageActivityThread.java:1449
                                                             at android.os.Handler.dispatchMessageHandler.java:102
                                                             at android.os.Looper.loopLooper.java:145
                                                             at android.app.ActivityThread.mainActivityThread.java:5981
                                                             at java.lang.reflect.Method.invokeNative Method
                                                             at java.lang.reflect.Method.invokeMethod.java:372
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:1399
                                                             at com.android.internal.os.ZygoteInit.mainZygoteInit.java:1194
                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method void com.xd.v2ex.utils.ApplicationController.addToRequestQueuecom.android.volley.Request on a null object reference
                                                             at com.xd.v2ex.activity.MainActivity.fetchMoviesMainActivity.java:116
                                                             at com.xd.v2ex.activity.MainActivity.onCreateMainActivity.java:79
                                                             at android.app.Activity.performCreateActivity.java:6289
                                                             at android.app.Instrumentation.callActivityOnCreateInstrumentation.java:1119
                                                             at android.app.ActivityThread.performLaunchActivityActivityThread.java:2655
                                                             at android.app.ActivityThread.handleLaunchActivityActivityThread.java:2767 
                                                             at android.app.ActivityThread.access$900ActivityThread.java:177 
                                                             at android.app.ActivityThread$H.handleMessageActivityThread.java:1449 
                                                             at android.os.Handler.dispatchMessageHandler.java:102 
                                                             at android.os.Looper.loopLooper.java:145 
                                                             at android.app.ActivityThread.mainActivityThread.java:5981 
                                                             at java.lang.reflect.Method.invokeNative Method 
                                                             at java.lang.reflect.Method.invokeMethod.java:372 
                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:1399 
                                                             at com.android.internal.os.ZygoteInit.mainZygoteInit.java:1194 

MainActivity

package com.xd.v2ex.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.xd.v2ex.R;
import com.xd.v2ex.adapter.LatestAdapter;
import com.xd.v2ex.bean.ItemInfo;
import com.xd.v2ex.utils.ApplicationController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private static final String TAG = MainActivity.class.getSimpleName;

private ArrayList<ItemInfo> itemInfos;
private LatestAdapter latestAdapter;
private ListView latestListView;

private static final String url = "http://www.v2ex.com/api/topics/latest.json";

private ProgressDialog pDialog;

@Override
protected void onCreateBundle savedInstanceState {
    super.onCreatesavedInstanceState;
    setContentViewR.layout.activity_main;
    Toolbar toolbar = Toolbar findViewByIdR.id.toolbar;
    setSupportActionBartoolbar;

    FloatingActionButton fab = FloatingActionButton findViewByIdR.id.fab;
    fab.setOnClickListenernew View.OnClickListener {
        @Override
        public void onClickView view {
            Snackbar.makeview, "Replace with your own action", Snackbar.LENGTH_LONG
                    .setAction"Action", null.show;
        }
    };

    DrawerLayout drawer = DrawerLayout findViewByIdR.id.drawer_layout;
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close;
    drawer.setDrawerListenertoggle;
    toggle.syncState;

    NavigationView navigationView = NavigationView findViewByIdR.id.nav_view;
    navigationView.setNavigationItemSelectedListenerthis;

    /**
    • listview
      */

         latestListView = ListView findViewByIdR.id.latestListView;
      
         itemInfos = new ArrayList<ItemInfo>;
      
         fetchMovies;
         latestAdapter = new LatestAdapterMainActivity.this, itemInfos;
      
         latestListView.setAdapterlatestAdapter;

      }

    1. void fetchMovies {

         JsonArrayRequest req = new JsonArrayRequesturl, new Response.Listener<JSONArray> {
      
             @Override
             public void onResponseJSONArray jsonArray {
      
                 for int i = 0; i < jsonArray.length; i++ {
                     try {
                         JSONObject object=jsonArray.getJSONObjecti;
                         ItemInfo itemInfo=new ItemInfo;
                         itemInfo.setTitleobject.getString"title";
                         itemInfo.setImage"http:"+object.getJSONObject"member".getString"avatar_large";
                         itemInfo.setNodeobject.getString"created";
                         itemInfo.setAuthorobject.getString"id";
      
                         itemInfos.additemInfo;
                     } catch JSONException e {
                         e.printStackTrace;
                     }
      
                 }
                 //ע��ˢ������
                 latestAdapter.notifyDataSetChanged;
             }
         }, new Response.ErrorListener {
             @Override
             public void onErrorResponseVolleyError volleyError {
                 Log.eTAG, "error:" + volleyError.getMessage;
             }
         };
         ApplicationController.getInstance.addToRequestQueuereq;

      }

      @Override

    2. void onBackPressed {

         DrawerLayout drawer = DrawerLayout findViewByIdR.id.drawer_layout;
         if drawer.isDrawerOpenGravityCompat.START {
             drawer.closeDrawerGravityCompat.START;
         } else {
             super.onBackPressed;
         }

      }

      @Override

    3. boolean onCreateOptionsMenuMenu menu {

         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater.inflateR.menu.main, menu;
         return true;

      }

      @Override

    4. boolean onOptionsItemSelectedMenuItem item {

         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId;
      
         //noinspection SimplifiableIfStatement
         if id == R.id.action_settings {
             return true;
         }
      
         return super.onOptionsItemSelecteditem;

      }

      @SuppressWarnings"StatementWithEmptyBody"
      @Override

    5. boolean onNavigationItemSelectedMenuItem item {

         // Handle navigation view item clicks here.
         int id = item.getItemId;
      
         if id == R.id.nav_camera {
             // Handle the camera action
         } else if id == R.id.nav_gallery {
      
         } else if id == R.id.nav_slideshow {
      
         } else if id == R.id.nav_manage {
      
         } else if id == R.id.nav_share {
      
         } else if id == R.id.nav_send {
      
         }
      
         DrawerLayout drawer = DrawerLayout findViewByIdR.id.drawer_layout;
         drawer.closeDrawerGravityCompat.START;
         return true;

      }
      }

    把fetchMovies方法调用放到latestListView.setAdapter之后

    ApplicationController.getInstance.addToRequestQueuereq; 既然是这句报错了,你就验证下前后是否是null的,打印下ApplicationController.getInstance 和 req 这两个,在调用前,不过看你req是在上面重新new的一个,所以ApplicationController.getInstance这个为null的可能性比较大。

    我快哭了…………我也是遇到这个问题,哥你是怎么解决的??? 跪求……

    该答案已被忽略,原因:

    广告位

    澳门赌场开户