【问题标题】:If statement trouble on static variable [duplicate]静态变量的if语句问题[重复]
【发布时间】:2013-09-24 17:13:28
【问题描述】:

伙计们,我在 android 上工作,我有一个静态变量,可以说“var”,如果它为 null,那么如果执行了语句,或者执行了 else,事情就是这样,即使在我“var”不为 null 之后,如果我重新启动选项卡它按预期工作,有人启发我。

**protected static String ipath="" , ipath1;**


 protected OnClickListener selimg = new OnClickListener() // for first image
{

    @Override
    public void onClick(View vw) 
    {
        // TODO Auto-generated method stub
        Intent i = new Intent(
                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                 startActivityForResult(i, RESULT_LOAD_IMAGE);

    }



};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.imageView1);
        ImageView imageView1 = (ImageView) findViewById(R.id.imageView2);
        **if(ipath == "")
        {
        ipath=picturePath;

        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
        else
        {
            ipath1=picturePath;
            imageView1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }**

    }
}

【问题讨论】:

  • 请出示相关代码。
  • 我认为这里的语法需要清理一下,不清楚,很难说出你的要求。
  • 这个空检查在哪里?
  • 这不是空检查。
  • 我认为这不能回答您的问题,但以下 if 语句中的 null 检查应该在语句的其余部分之前:if (null != data && requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK)

标签: java if-statement static


【解决方案1】:

if(ipath == "")改成if(ipath.isEmpty())或者你也可以把它改成if(ipath.equals("")),我建议阅读This

【讨论】:

  • 伙计们,我想通了,我必须使用 onDestroy() 方法,以便在退出前清理资源!!!!
猜你喜欢
  • 2019-04-21
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2017-10-25
  • 2016-11-21
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
相关资源
最近更新 更多