Android应用程序在解析json时关闭

时间:2016-12-18 20:45:46

标签: android json android-volley

我是android和stackoverflow的新手。

我有一个带有2个按钮和1个textview的android工作室项目,用于解析带有齐射的json(对象和数组)。

我的应用程序编译没有任何错误并运行。但当我点击我的一个按钮(对于解析对象和数组)时,我的应用程序就会关闭! 我的MainActivity,myVolleyController和清单文件:

   package net.aminapps.volleyjson;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private static final String URLJsonObject="http://api.androidhive.info/volley/person_object.json";
    private static final String URLJsonArry = "http://api.androidhive.info/volley/person_array.json";
    private static String TAG = MainActivity.class.getSimpleName();
    public Button btnMakeObjectRequest,btnMakeArrayRequest;
    private ProgressDialog pDialog;
    private TextView txtResponse;
    private String jsonResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
        btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
        txtResponse = (TextView) findViewById(R.id.txtResponse);
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeJsonObjectRequest();
            }
        });
        btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeJsonArrayRequest();
            }
        });
    }
    private void makeJsonObjectRequest() {
        showpDialog();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                URLJsonObject, null, new Response.Listener<JSONObject>() {
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());
                try {
                    String name = response.getString("name");
                    String email = response.getString("email");
                    JSONObject phone = response.getJSONObject("phone");
                    String home = phone.getString("home");
                    String mobile = phone.getString("mobile");
                    jsonResponse = "";
                    jsonResponse += "Name: " + name + "\n\n";
                    jsonResponse += "Email: " + email + "\n\n";
                    jsonResponse += "Home: " + home + "\n\n";
                    jsonResponse += "Mobile: " + mobile + "\n\n";

                    txtResponse.setText(jsonResponse);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
                hidepDialog();
            }
        }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });
        myVolleyController.getInstance().addToRequestQueue(jsonObjReq);
    }
        private void makeJsonArrayRequest() {
            showpDialog();
            JsonArrayRequest req = new JsonArrayRequest(URLJsonArry,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.d(TAG, response.toString());
                            try {
                                jsonResponse = "";
                                for (int i = 0; i < response.length(); i++) {
                                    JSONObject person = (JSONObject) response.get(i);
                                    String name = person.getString("name");
                                    String email = person.getString("email");
                                    JSONObject phone = person
                                            .getJSONObject("phone");
                                    String home = phone.getString("home");
                                    String mobile = phone.getString("mobile");

                                    jsonResponse += "Name: " + name + "\n\n";
                                    jsonResponse += "Email: " + email + "\n\n";
                                    jsonResponse += "Home: " + home + "\n\n";
                                    jsonResponse += "Mobile: " + mobile + "\n\n\n";

                                }
                                txtResponse.setText(jsonResponse);

                            } catch (JSONException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(),
                                        "Error: " + e.getMessage(),
                                        Toast.LENGTH_LONG).show();
                            }

                            hidepDialog();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    hidepDialog();
                }
            });
            myVolleyController.getInstance().addToRequestQueue(req);
        }

        private void showpDialog() {
            if (!pDialog.isShowing())
                pDialog.show();
        }

        private void hidepDialog() {
            if (pDialog.isShowing())
                pDialog.dismiss();
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem 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.onOptionsItemSelected(item);
    }
}

package net.aminapps.volleyjson;

import android.app.Application;
import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * Created by My PC on 12/14/2016.
 */
public class myVolleyController extends Application {
    public static final String TAG = myVolleyController.class.getSimpleName();
    private RequestQueue myRQ;
    private static myVolleyController myInstance;

    public void onCreate() {
        super.onCreate();
        myInstance = this;
    }

    public static synchronized myVolleyController getInstance() {
        return myInstance;
    }

    public RequestQueue getRequestQueue() {
        if (myRQ == null) {
            myRQ = Volley.newRequestQueue(getApplicationContext());
        }

        return myRQ;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        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 (myRQ != null) {
            myRQ.cancelAll(tag);
        }

    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.aminapps.volleyjson">

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest> 

这是我的logcat(我检查但无法理解问题出在哪里!):

    12-19 01:26:16.111 31003-31003/net.aminapps.volleyjson I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
12-19 01:26:16.121 31003-31003/net.aminapps.volleyjson I/InjectionManager: Inside getClassLibPath caller 
12-19 01:26:16.161 31003-31003/net.aminapps.volleyjson D/InjectionManager: InjectionManager
12-19 01:26:16.161 31003-31003/net.aminapps.volleyjson D/InjectionManager: fillFeatureStoreMap net.aminapps.volleyjson
12-19 01:26:16.161 31003-31003/net.aminapps.volleyjson I/InjectionManager: Constructor net.aminapps.volleyjson, Feature store :{}
12-19 01:26:16.161 31003-31003/net.aminapps.volleyjson I/InjectionManager: featureStore :{}
12-19 01:26:16.241 31003-31003/net.aminapps.volleyjson D/SecWifiDisplayUtil: Metadata value : SecSettings2
12-19 01:26:16.271 31003-31003/net.aminapps.volleyjson W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-19 01:26:16.311 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* installDecor mIsFloating : false
12-19 01:26:16.311 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* installDecor flags : -2139029248
12-19 01:26:16.391 31003-31003/net.aminapps.volleyjson D/Activity: performCreate Call Injection manager
12-19 01:26:16.391 31003-31003/net.aminapps.volleyjson I/InjectionManager: dispatchOnViewCreated > Target : net.aminapps.volleyjson.MainActivity isFragment :false
12-19 01:26:16.391 31003-31037/net.aminapps.volleyjson D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-19 01:26:16.411 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
12-19 01:26:16.411 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
12-19 01:26:16.461 31003-31003/net.aminapps.volleyjson D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
12-19 01:26:16.501 31003-31037/net.aminapps.volleyjson D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
12-19 01:26:16.531 31003-31037/net.aminapps.volleyjson D/libEGL: eglInitialize EGLDisplay = 0x7f85ba08b8
12-19 01:26:16.531 31003-31037/net.aminapps.volleyjson I/OpenGLRenderer: Initialized EGL, version 1.4
12-19 01:26:16.551 31003-31037/net.aminapps.volleyjson I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0x7f9401b940 ,&mEglDisplay = 1 , &mEglConfig = -1810767184 
12-19 01:26:16.551 31003-31037/net.aminapps.volleyjson D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
12-19 01:26:16.551 31003-31037/net.aminapps.volleyjson D/OpenGLRenderer: Enabling debug mode 0
12-19 01:26:16.551 31003-31037/net.aminapps.volleyjson D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
12-19 01:26:16.571 31003-31003/net.aminapps.volleyjson V/ActivityThread: updateVisibility : ActivityRecord{b3f715 token=android.os.BinderProxy@6df2a88 {net.aminapps.volleyjson/net.aminapps.volleyjson.MainActivity}} show : true
12-19 01:26:16.771 31003-31003/net.aminapps.volleyjson I/InjectionManager: dispatchCreateOptionsMenu :net.aminapps.volleyjson.MainActivity
12-19 01:26:16.771 31003-31003/net.aminapps.volleyjson I/InjectionManager: dispatchPrepareOptionsMenu :net.aminapps.volleyjson.MainActivity
12-19 01:26:16.811 31003-31003/net.aminapps.volleyjson W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
12-19 01:26:16.811 31003-31003/net.aminapps.volleyjson I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@6df2a88 time:338081977
12-19 01:26:37.901 31003-31003/net.aminapps.volleyjson I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@6df2a88 time:338103064
12-19 01:26:38.631 31003-31003/net.aminapps.volleyjson D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
12-19 01:26:39.151 31003-31003/net.aminapps.volleyjson V/ActivityThread: updateVisibility : ActivityRecord{b3f715 token=android.os.BinderProxy@6df2a88 {net.aminapps.volleyjson/net.aminapps.volleyjson.MainActivity}} show : false
12-19 01:26:41.751 31003-31003/net.aminapps.volleyjson D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
12-19 01:26:41.751 31003-31037/net.aminapps.volleyjson D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
12-19 01:26:41.791 31003-31003/net.aminapps.volleyjson I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@6df2a88 time:338106951
12-19 01:26:43.461 31003-31003/net.aminapps.volleyjson D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
12-19 01:26:45.231 31003-31003/net.aminapps.volleyjson D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
12-19 01:26:45.341 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* installDecor mIsFloating : true
12-19 01:26:45.341 31003-31003/net.aminapps.volleyjson D/PhoneWindow: *FMB* installDecor flags : 8388610
12-19 01:26:45.371 31003-31017/net.aminapps.volleyjson I/art: Background sticky concurrent mark sweep GC freed 13230(2MB) AllocSpace objects, 3(60KB) LOS objects, 41% free, 3MB/5MB, paused 6.406ms total 28.855ms
12-19 01:26:45.391 31003-31003/net.aminapps.volleyjson D/AndroidRuntime: Shutting down VM
12-19 01:26:45.391 31003-31003/net.aminapps.volleyjson E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: net.aminapps.volleyjson, PID: 31003
                                                                         java.lang.NullPointerException: Attempt to invoke virtual method 'void net.aminapps.volleyjson.myVolleyController.addToRequestQueue(com.android.volley.Request)' on a null object reference
                                                                             at net.aminapps.volleyjson.MainActivity.makeJsonObjectRequest(MainActivity.java:112)
                                                                             at net.aminapps.volleyjson.MainActivity.access$000(MainActivity.java:30)
                                                                             at net.aminapps.volleyjson.MainActivity$2.onClick(MainActivity.java:65)
                                                                             at android.view.View.performClick(View.java:5254)
                                                                             at android.widget.TextView.performClick(TextView.java:10557)
                                                                             at android.view.View$PerformClick.run(View.java:21203)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:145)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6897)
                                                                             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:1404)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
12-19 01:26:48.161 31003-31003/net.aminapps.volleyjson I/Process: Sending signal. PID: 31003 SIG: 9

2 个答案:

答案 0 :(得分:1)

如果你正在使用单身myVolleyController,那么你可能还没有被添加到清单文件中。

<application
        android:name="*your package*.myVolleyController">
.
.
.
.

</application>

答案 1 :(得分:0)

您可以在logcat中找到错误。可以在Android选项卡下的屏幕底部找到logcat。

logcat location

修改 我相信你的问题在这里:     。myVolleyController.getInstance()addToRequestQueue(REQ);

getInstance()方法似乎返回null,你永远不会创建你的对象。

编辑2:

public static synchronized myVolleyController getInstance() {
if(myInstance == null){
    myInstance = new myVolleyController();
}
    return myInstance;
}