Java 调用onResponse时应用程序崩溃

Java 调用onResponse时应用程序崩溃,java,android,stack-trace,Java,Android,Stack Trace,我正在使用volley加载JSON数据 这是主要活动 public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { private final String TAG = "MainActivity"; //Creating a list of posts private List<P

我正在使用volley加载JSON数据

这是主要活动

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private final String TAG = "MainActivity";


    //Creating a list of posts
    private List<PostItems> mPostItemsList;

    //Creating Views
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Device rotated and onCreate called");

        //Initializing Views
        recyclerView = (RecyclerView) findViewById(R.id.post_recycler);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);


        //Initializing the postlist
        mPostItemsList = new ArrayList<>();
        adapter = new PostAdapter(mPostItemsList, this);

        recyclerView.setAdapter(adapter);

        if (NetworkCheck.isAvailableAndConnected(this)) {
            //Caling method to get data
            getData();
        } else {
            final Context mContext;
            mContext = this;
            final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("No Internet Connection");
            alertDialogBuilder.setMessage("Failed to load. Please ensure you're connected to the internet and try again.");
            alertDialogBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (!NetworkCheck.isAvailableAndConnected(mContext)) {
                        alertDialogBuilder.show();
                    } else {
                        getData();
                    }


                }
            });
            alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });
            alertDialogBuilder.show();

        }

    }

    //This method will get data from the web api
    private void getData(){

        Log.d(TAG, "getData called");
        //Showing progress dialog
        final ProgressDialog progressDialog = ProgressDialog.show(this, null, "Loading posts", false, false);

        //Creating a json request
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigPost.GET_URL,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, "onResponse called");
                        //Dismissing the progress dialog
                        progressDialog.dismiss();


                        //calling method to parse json array
                        parseData(response);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //Creating request queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);

        //Adding request to the queue
        requestQueue.add(jsonArrayRequest);
    }


}
从stacktrace中,第142行是
progressDialog.Disclease()第137行是
新响应。侦听器(){

你知道是什么原因造成的吗?还有解决办法吗


更新:我正在考虑对其进行编码,以便只有在第一次创建应用程序时才会调用getData()。这是因为,在配置更改(在本例中为旋转)后重新创建活动时才会调用getData()。因此,这会导致活动重新加载。这可能吗?即调用getData()只有第一次创建活动。

在关闭
progressdialog
时,您只需检查is object
null

    ProgressDialog progressDialog = ProgressDialog.show(this, null, "Loading posts", false, false);

 if(progressDialog!=null)
progressDialog.dismiss();

当关闭
progressdialog
时,只需检查is object
null

    ProgressDialog progressDialog = ProgressDialog.show(this, null, "Loading posts", false, false);

 if(progressDialog!=null)
progressDialog.dismiss();

当应用程序旋转时,
活动
被重新创建
。现在您得到了
ProgressDialog
ProgressDialog
的实例。因此,当活动旋转时,旧活动被销毁,因此此实例
ProgressDialog
被销毁

现在,任务正在后台线程上运行,当它返回时,找不到
progressDialog
,因为它已被销毁,因此当前
未连接到窗口管理器
IllegalArgumentException

因此,在访问实例
onResponse
之前,最好先检查该实例
progressDialog

试着像这样使用
ProgressDialog

定义全局实例:
private ProgressDialog;

getData
方法中:

private void getData(){

    Log.d(TAG, "getData called");
    //Showing progress dialog
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Loading posts");
    progressDialog.show();
onResponse
回调中:

if(progressDialog!=null){
    progressDialog.hide();
}
活动的onDestroy

if(progressDialog!=null) progressDialog.dismiss(); // to prevent memory leak

当应用程序旋转时,
活动
被重新创建
。现在您得到了
ProgressDialog
ProgressDialog
的实例。因此,当活动旋转时,旧活动被销毁,因此此实例
ProgressDialog
被销毁

现在,任务正在后台线程上运行,当它返回时,找不到
progressDialog
,因为它已被销毁,因此当前
未连接到窗口管理器
IllegalArgumentException

因此,在访问实例
onResponse
之前,最好先检查该实例
progressDialog

试着像这样使用
ProgressDialog

定义全局实例:
private ProgressDialog;

getData
方法中:

private void getData(){

    Log.d(TAG, "getData called");
    //Showing progress dialog
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Loading posts");
    progressDialog.show();
onResponse
回调中:

if(progressDialog!=null){
    progressDialog.hide();
}
活动的onDestroy

if(progressDialog!=null) progressDialog.dismiss(); // to prevent memory leak

把你的parseData代码放进去,但是stacktrace没有提到parseData?真的需要吗?如果我错了,请纠正我。更改`final ProgressDialog ProgressDialog=ProgressDialog.show(这个,null,“Loading posts”,false,false);`to
OnCreate()
@Kathi,如果我这样做,progressdialog不是每次重新创建活动时都会弹出吗?对不起,不要显示它
\onCreate()
,只显示
progressdialog progress=new progressdialog(此);
并在
getData()中的
OnResponse()
之前显示它
放入您的parseData代码,但是stacktrace没有提到parseData?它真的需要吗?如果我错了,请纠正我。将`final ProgressDialog ProgressDialog=ProgressDialog.show(这个,null,“Loading posts”,false,false);`to
OnCreate()
@Kathi,如果我这样做,progressdialog不是每次重新创建活动时都会弹出吗?对不起,不要显示它
\onCreate()
,只显示
progressdialog progress=new progressdialog(此);
并在
getData()中的
OnResponse()
之前显示它
我该怎么做?请注意,我已经更新了这个问题。谢谢先生!它像魔术一样工作!但我有几个问题:为什么我们不直接从
onresponse
调用
progressDialog.dismis()
并在
onresponse
中忘记它?其次,我不能停止
获取数据()
在配置更改后重新创建活动时被调用?我该怎么做?请注意,我已经更新了这个问题。谢谢,先生!它像魔术一样工作!但我有几个问题:我们为什么不调用
progressDialog.dismis()
直接从
onresponse
开始,并在
onDestroy
中忘记它?其次,在配置更改后重新创建活动时,我不能停止调用
getData()