在使用结果之前检查意图是否为空

时间:2015-05-18 13:32:41

标签: android android-intent

我在onNewIntent方法中从onPostExecute方法获取了一个intent。有时这个意图有价值,然后我才想显示我的对话窗口但是当前警报显示在方法中,即使意图为空?我怎么能避免这种情况,因为我已经检查了额外的意图。

protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);

    Bundle extras = getIntent().getExtras();
    if (extras != null && extras.containsKey("stop_route")) {

        ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
           .
           .
           .

    }
}

onPostExecute方法:

@Override
protected void onPostExecute(Void result) {
    // Intent with Conetxt of the Asyntask class and
    Intent intent = new Intent(mContext, MainActivity.class);
    intent.putIntegerArrayListExtra("stop_route", routes);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    mContext.startActivity(intent);

}

1 个答案:

答案 0 :(得分:1)

您可以尝试检查: extras.getIntegerArrayList(“stop_route”)。length&gt;当结果返回到if条件时为0。

相关问题