【问题标题】:Android activity unresponsive until back button is pressed在按下后退按钮之前,Android 活动无响应
【发布时间】:2015-02-24 11:58:15
【问题描述】:

以下是我的活动的 oncreate。我的问题是,当活动开始时,它完全没有响应,直到我按下后退按钮。一旦我按下后退按钮,它就会完美运行。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_packs);
    count = 0;

    Bundle extras = getIntent().getExtras();
    if(extras != null || !equals("")){
        name = extras.getString("name");
    }
    else{name="PackActivity";}


    //getActionBar().setTitle(name);  
    ActionBar actionBar = getActionBar();
    //actionBar.setBackgroundDrawable(R.drawable.navigation_bar_colour_image);
    //actionBar.setHomeButtonEnabled(true);
    actionBar.setTitle(name);

    actionBar.setDisplayHomeAsUpEnabled(false);
    //actionBar.hide();
    database = new DataObjectDataSource(this.getApplicationContext());
    //load all the packs from the DB
    packs = loadPacks();
    //make the request for GetPacks
    sortArrayById();

    if(isOnline(this)) {
        HTTPRequest.getHTTPRequest(HTTPRequest.getPacksURL, this);
    }
    else{
        dialog("No internet connection available","their is limited functionality available in offline mode",1);
    }


    gridView = (GridView) findViewById(R.id.packGrid);

    adapter = new PackGridAdapter(this, getApplicationContext(), packs);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(this);

    System.out.println(" pack_ids ");
}

我已经包含了对话框功能,因为在它被关闭后会出现无响应。

public boolean dialog(String mes,String message,int type){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Add the buttons
    if(type==2){
        builder.setMessage(message)
        .setTitle(mes);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });
    }
    // Create the AlertDialog
    final AlertDialog dialog = builder.create();
    dialog.show();
    if(type==2){
        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                dialog.dismiss();
                // when the task active then close the dialog
                t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
            }
        }, 5000);

    }
    return true;
}

【问题讨论】:

  • 很明显,UI 的脚步不知何故停止了。您是否检查了哪一行代码后 UI 线程停止了?
  • 我刚刚想通了哈哈。典型的!被调用的对话框函数没有正确关闭对话框。无论如何,感谢您的关注。

标签: java android android-activity input oncreate


【解决方案1】:

两件事:

首先确认您的ActionBar 代码正在运行(评论actionbar 部分以确保这不是这里的罪魁祸首)以查看活动是否响应

如果第一个不起作用,并且即使在评论 ActionBar 之后活动也没有响应.. 那么

第二条评论这些行:

if(isOnline(this)) {
    HTTPRequest.getHTTPRequest(HTTPRequest.getPacksURL, this);
}
else{
    dialog("No internet connection available","their is limited functionality available in offline mode",1);
}

我怀疑你在你的 UI 线程上做一些网络操作,这可能是 Activity 没有响应的原因,或者它一定与你正在使用的 dialog 方法有关。如果您显示该方法代码,它可能会导致进一步诊断。

【讨论】:

  • 我刚刚弄明白了。我的对话框功能没有正确关闭对话框。不过,你是在正确的路线上。感谢收看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2013-08-08
  • 1970-01-01
相关资源
最近更新 更多