Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 应用程序因IllegalArgumentException而崩溃:com.android.internal.policy.impl.PhoneWindow_Java_Android - Fatal编程技术网

Java 应用程序因IllegalArgumentException而崩溃:com.android.internal.policy.impl.PhoneWindow

Java 应用程序因IllegalArgumentException而崩溃:com.android.internal.policy.impl.PhoneWindow,java,android,Java,Android,基于webservice的响应,我决定打开活动 if(getCompanyByUser().size() > 0) { Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Intent intent = new Intent(SplashScreenActivity.this,

基于webservice的响应,我决定打开活动

if(getCompanyByUser().size() > 0) {
    Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
} else {
    Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
    startActivity(intent);
    finish();
}
getCompanyByUser()就是我调用webservice的方法

private ArrayList<CompApp> getCompanyByUser() {
    final ArrayList<CompApp> list = new ArrayList<CompApp>();
    showpDialog(pDialog);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, getCompByUser,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("response",response);
                    try{
                        CompApp[] access = new Gson().fromJson(response, CompApp[].class);
                        for (CompApp c : access) {
                            list.add(c);
                        }

                        helper.addCompany(list);
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
                        String dateString = formatter.format(new java.util.Date());
                        saveSharedPref(SplashScreenActivity.this, "timestamp", dateString);
                    }catch (Exception e) {
                        Toast.makeText(SplashScreenActivity.this, "Unable to process request.\nTry again.", Toast.LENGTH_SHORT).show();
                    }
                    hidepDialog(pDialog);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(SplashScreenActivity.this, "Internet not connected.", Toast.LENGTH_LONG).show();
                    hidepDialog(pDialog);
                }
            }) {
        @Override
        protected Map<String,String> getParams() {
            Map<String,String> params = new HashMap<String, String>();
            User user = new Gson().fromJson(getSharePref(SplashScreenActivity.this, "user"), User.class);
            String lastUpdated = getSharePref(SplashScreenActivity.this, "lastUpdated");
            params.put("appCode","PCA001");
            params.put("userId",user.id);
            params.put("timestamp",""+lastUpdated);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(SplashScreenActivity.this);
    requestQueue.add(stringRequest);
    return list;
}

这里的代码流不正确。调用getCompanyByUser()时,它是一个异步调用。因此,在继续之前,您需要等待它的响应

因此,只需调用
getcompanybyyuser()
。将其返回类型更改为void,因为这不是必需的,因为它是一个异步调用。现在,您应该在onResponse和ErrorResponse回调中开始您的活动

private void getCompanyByUser() {
//....
@Override
public void onResponse(String response) {
 // ...
//Once the list is made start new activity
if(list.size() > 0) {
    Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
    startActivity(intent);
    finish();
} else {
    Intent intent = new Intent(SplashScreenActivity.this, SelectCompany.class);
    startActivity(intent);
    finish();
}
}
}

你只有你的报税表有问题。您将在列表填充到
onResponse()
之前返回,因为
onResponse
是异步的,所以您不必等待它并返回空列表。其次,异常是因为您正在执行一些对话框操作(您将保留活动的引用),以响应
onResponse()
方法,但当前活动已经完成,因此引发异常。 最好的解决方案是使用一个处理程序,带有成功和失败的消息。在您的响应/失败和处理程序的
handleMessage(Message msg)
中向处理程序发送回调,以决定应该启动哪个活动

大概是这样的: 在您的活动中,创建如下处理程序

Handler handler =new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
      // you may check success or failure on msg based. 
      // handle your next activity check here
    } 
  }; 
并在您的
onResponse()
onFailure()
中向类似的处理程序发送消息
handler.sendEmptyMessage()
handler.sendMessage(msg)

显示清单文件可以放置
hidepDialog
方法。问题就在那里,谢谢。现在我非常清楚web服务调用。
Handler handler =new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
      // you may check success or failure on msg based. 
      // handle your next activity check here
    } 
  };