com.android.volley.VolleyError:java.lang.NullPointerException

com.android.volley.VolleyError:java.lang.NullPointerException,第1张

概述我读过了WhatisaNullPointerException,andhowdoIfixit?Attempttoinvokevirtualmethod‘intjava.lang.String.length()’onanullobjectreference还有一些,但是我没有运气修复它.这是我从齐射错误监听器收到的错误-com.android.volley.VolleyError:java.lang.N

我读过了
What is a NullPointerException, and how do I fix it?
Attempt to invoke virtual method ‘int java.lang.String.length()’ on a null object reference
还有一些,但是我没有运气修复它.

这是我从齐射错误监听器收到的错误-

com.androID.volley.VolleyError:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法’int java.lang.String.length()’

我是androID的新手,有人可以向我解释我的问题的原因并帮助我解决它吗?

以下是发出截击请求的活动代码-

public class CircleActivity extends AppCompatActivity  implements SwipeRefreshLayout.OnRefreshListener {private Toolbar toolbar;private floatingActionbutton fab;private ArrayList<Users> ListUsers = new ArrayList<>();SessionManager sessionManager;broadcastReceiver broadcastReceiver;private RecyclerVIEw ListUsersCircle;//swipeprivate SwipeRefreshLayout swipeRefreshLayout;//volley login//mobile hotspot//public static final String REGISTER_URL = "http://192.168.43.181/save/user/getPeopleList";//gharpublic final String REGISTER_URL = "http://192.168.0.10/save/user/getPeopleList";private Circlelistadapter circlelistadapter;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    sessionManager = new SessionManager(getApplicationContext());    if (sessionManager.isLoggedIn()) {        setContentVIEw(R.layout.activity_circle);        toolbar = (Toolbar) findVIEwByID(R.ID.app_bar);        //tell androID to use my toolbar        setSupportActionbar(toolbar);        getSupportActionbar().setdisplayHomeAsUpEnabled(true);        NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)                getSupportFragmentManager().findFragmentByID(R.ID.fragment_navigation_drawer);        drawerFragment.setUp(R.ID.fragment_navigation_drawer, (DrawerLayout) findVIEwByID(R.ID.drawer_layout), toolbar);        ListUsersCircle = (RecyclerVIEw) findVIEwByID(R.ID.circle);        ListUsersCircle.setLayoutManager(new linearlayoutmanager(getApplicationContext()));        circlelistadapter = new Circlelistadapter(getApplicationContext());        ListUsersCircle.setAdapter(circlelistadapter);        swipeRefreshLayout = (SwipeRefreshLayout) findVIEwByID(R.ID.swipe_refresh_circle);        swipeRefreshLayout.setonRefreshListener(this);        sendJsonRequest();        //get user_ID from session        String user_ID = sessionManager.getUserDetails().get("user_ID");        //adding data to the intent        Intent intent = new Intent(getBaseContext), LocationService.class);        intent.putExtra("user_ID", user_ID);        startService(intent);        fab = (floatingActionbutton) findVIEwByID(R.ID.fab);        fab.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                String phoneNumber="123123123412";                String message ="Hello sms";                sendSMS(phoneNumber,message);            }        });        //location service        // startService(new Intent(getBaseContext(), LocationService.class));        //swipe to refresh   /* JsONObject obj = new JsONObject();    try {        obj.put("txtCircleID", "acca16d632");    } catch (JsONException e) {        e.printstacktrace();    }    Log.d("Nishon",obj.toString());    RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,            URL_GET_CIRCLE_USERS,            obj,            new Response.Listener<JsONObject>() {        @OverrIDe        public voID onResponse(JsONObject response) {            Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();            Log.d("Nishon",response.toString());        }    }, new Response.ErrorListener() {        @OverrIDe        public voID one rrorResponse(VolleyError error) {            Log.d("Nishon", error.toString());        }    });    requestQueue.add(request);*/    } else {        Intent intent = new Intent(this, LoginActivity.class);        startActivity(intent);        finish();    }}private voID sendSMS(String phoneNumber, String message){    PendingIntent pi = PendingIntent.getActivity(this, 0,            new Intent(this, SMS.class), 0);    SmsManager sms = SmsManager.getDefault();    sms.sendTextMessage(phoneNumber, null, message, pi, null);}public voID sendJsonRequest() {    RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();    StringRequest request = new StringRequest(Request.Method.POST, REGISTER_URL,            new Response.Listener<String>() {                @OverrIDe                public voID onResponse(String response) {                    ListUsers = parseJsonResponse(response);                    circlelistadapter.setCircleList(ListUsers);                    Log.d("d",response);                }            },            new Response.ErrorListener() {                @OverrIDe                public voID one rrorResponse(VolleyError error) {                    Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();                    Log.d("d", error.toString());                }            }) {        @OverrIDe        protected Map<String, String> getParams() {            Map<String, String> params = new HashMap<String, String>();            String group_key= sessionManager.getUserDetails().get("group_key");            params.put("group_key", group_key);            return params;        }    };    requestQueue.add(request);}public ArrayList<Users> parseJsonResponse(String response) {    ArrayList<Users> ListUsers = new ArrayList<>();    if (response != null || response.length() > 0) {        Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();        JsONObject JsonObject = null;        try {            JsonObject = new JsONObject(response.toString());        } catch (JsONException e) {            e.printstacktrace();        }        //extracting Json array from response string        JsONArray JsonArray = null;        try {            if (JsonObject.has("users")) {                JsonArray = JsonObject.getJsONArray("users");                for (int z = 0; z < JsonArray.length(); z++) {                    JsONObject currentUser = JsonArray.getJsONObject(z);                    //String email = currentUser.getString("email");                    String fname = currentUser.getString("first_name");                    String lname = currentUser.getString("last_name");                    Users user = new Users();                    user.setFname(fname);                    user.setLname(lname);                    ListUsers.add(user);                }            }        } catch (JsONException e) {            e.printstacktrace();        }       /* JsONObject JsonRow = null;        try {            JsonRow = JsonArray.getJsONObject(1);        } catch (JsONException e) {            e.printstacktrace();        }        //get value from JsonRow        try {            String resultStr = JsonRow.getString("email");            Log.d("Nishon", resultStr);        } catch (JsONException e) {            e.printstacktrace();        }*/    }    return ListUsers;}@OverrIDeprotected voID onPause() {    super.onPause();    finish();}public voID showNotification(VIEw vIEw) {}@OverrIDepublic voID onRefresh() {    swipeRefreshLayout.setRefreshing(true);    sendJsonRequest();    swipeRefreshLayout.setRefreshing(false);}/*@OverrIDepublic boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_POWER) {        // Do something here...        event.startTracking(); // Needed to track long presses        return true;    }    return super.onKeyDown(keyCode, event);}*/}

Web服务正在运行.

解决方法:

这意味着您正在尝试获取null的String的length().

罪魁祸首是这条线:

if (response != null || response.length() > 0) {

这意味着如果响应不为null或长度大于零.

您想要的是:

if (response != null && response.length() > 0) {

如果响应为null,则该语句将失败,因为如果为null,则无法检查长度.

总结

以上是内存溢出为你收集整理的com.android.volley.VolleyError:java.lang.NullPointerException全部内容,希望文章能够帮你解决com.android.volley.VolleyError:java.lang.NullPointerException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1120712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存