Android活动无响应,直到按下后退按钮

时间:2015-02-24 11:58:15

标签: java android android-activity input 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;
}

1 个答案:

答案 0 :(得分:1)

2件事:

首先确认您的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 not Responding的原因,或者它必须与你正在使用的dialog方法有关。如果您显示该方法代码,则可能会导致进一步诊断。