首页 > 解决方案 > Android 活动生命周期处理错误

问题描述

美好的一天,所以我有这个程序,用户将在其中扫描 QR 码并在 SQLite 数据库中获取它的值,但在我得到值之前,对话框弹出消息告诉我“没有找到参与者”。下面是我的代码:

这是我的 onStart 覆盖。

     @Override
protected void onStart()
{
    super.onStart();
    getparticipantid = tvParticipantID.getText().toString();
    etparticipantidvalue.setText(getparticipantid);
    getParticipantIbeam();
}

这是我的活动,一旦用户扫描二维码,它将从数据库中加载数据。

    private void getParticipantIbeam()
{
    SQLiteFunctionUlitity.GameIBeamParticipant memberInfo = SQLiteFunctionUlitity.getparticipantIbeam(getparticipantid, mDbHelper.getSqliteObjectWithReadable());
    if (memberInfo != null)
    {
        tvParticipantName.setText(memberInfo.getParticipantName());
        tvParticipantNation.setText(memberInfo.getParticipant_nationality());
        tvParticipantCategory.setText(memberInfo.getCategory());

        tvWave.setText(memberInfo.getWave_number());
        tvCategory.setText(memberInfo.getCategory());
    }
    else
    {
        Context context = this;
        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
        builder1.setMessage("No Participant found in the Game.");
        builder1.setCancelable(true);

        builder1.setPositiveButton("Scan Again", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                Intent intent = new Intent(GameiBeamLineTracing.this, ScanGameIbeam.class);
                startActivity(intent);
                finish();
            }
        });
        builder1.setNegativeButton("Close", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                onStop();
            }
        });
        AlertDialog alert11 = builder1.create();
        alert11.show();
    }
}

如您所见,我在 onStart 方法中调用 getParticipantIbeam 活动,但是当我调用它时,它读取的是 else 语句而不是 if 语句。

我知道这是一个简单的 if else 语句和生命周期,但我很想知道答案。

标签: androidsqliteqr-codeandroid-lifecycle

解决方案


我已经使用了扫描二维码的库,它非常简单,它可以帮助你。

安卓二维码实现库,点击这里(blikoon)

这个库绝对可以解决您所有与二维码相关的问题,并且不要忘记实现运行时权限,如照片、图库等。


推荐阅读