简体   繁体   English

编辑网址-怎么做?

[英]editing an URL - how to?

I am working in this code where I want to edit an Url with an EditText and when I press the button that brings me the image. 我在这段代码中工作,我想用EditText编辑一个Url,当我按下带给我图像的按钮时。 But I'm getting a mistake that saids that the EditText is sending a null value. 但是我遇到一个错误,说EditText发送的是空值。

public class MainActivity extends AppCompatActivity {

ImageView img;
Bitmap bitmap;
private Button btn;
EditText et_num;

String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num+".png";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.img);
    et_num = (EditText) findViewById(R.id.et_num);
    btn = (Button) findViewById(R.id.btn);

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new GetImageFromURL(img).execute(URLIMAGE);
        }
    });
}

public class GetImageFromURL extends AsyncTask<String, Void, Bitmap> {
    ImageView imgV;

    public GetImageFromURL(ImageView imgV) {
        this.imgV = imgV;
    }

    @Override
    protected Bitmap doInBackground(String... url) {
        String urldisplay = url[0];
        bitmap = null;
        try {
            InputStream srt = new java.net.URL(urldisplay).openStream();
            bitmap = BitmapFactory.decodeStream(srt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        imgV.setImageBitmap(bitmap);
     }
   }
 }

I'm getting this: 我得到这个:

 W/System.err: java.io.FileNotFoundException: https://www.avansys.edu.pe/sites/default/files/null.png

Does anyone has an idea of ​​how can I fix it? 有谁知道我该如何解决?

You got wrong here: 您在这里错了:

EditText et_num;
String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num+".png";

change to something like this: 更改为以下内容:

String URLIMAGE = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.img);
    et_num = (EditText) findViewById(R.id.et_num);
    btn = (Button) findViewById(R.id.btn);

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
     URLIMAGE  = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText().toString()+".png";
            new GetImageFromURL(img).execute(URLIMAGE);
        }
    });
}

Added 添加

Remember: You need to check nullPointerException for textView any time you want to get its value. 切记:每当您想获取其值时,都需要检查nullPointerException是否有textView。

if(!TextUtils.isEmpty(edt.getText().toString())){
 // Doing something
}

I see two things wrong here. 我在这里看到两件事是错的。

First, you're hardcoding URLIMAGE at initialization, which means it'll take whatever the et_num variable is at that point and convert it to a String. 首先,你硬编码URLIMAGE在初始化,这意味着它会采取任何的et_num变量是在这一点上 ,并将其转换为字符串。 Since it's null, the String representation is null . 由于为null,因此String表示形式为null

The second problem is that EditText#toString() isn't how you get the text from an EditText instance. 第二个问题是EditText#toString()并不是您从EditText实例获取文本的方式。 You should be using EditText#getText().toString() . 您应该使用EditText#getText().toString() So do two things: 所以做两件事:

  1. Since you're only using URLIMAGE in one place, just move the assignment to your click listener. 由于您仅在一个地方使用URLIMAGE ,因此只需将分配移至您的点击监听器即可。

  2. Use et_num.getText() not et_num . 使用et_num.getText()而不是et_num

Your onClick() code should look like this: 您的onClick()代码应如下所示:

@Override
public void onClick(View v) {
    String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText()+".png";
   new GetImageFromURL(img).execute(URLIMAGE);
}

1.Place String URLIMAGE; 1.放置字符串URLIMAGE; (before onCreate) (在onCreate之前)

2.Place you code inside btn.setOnClickListener also you need to get text from edit text. 2.将代码放在btn.setOnClickListener还需要从编辑文本中获取文本。

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText().toString()+".png";

            new GetImageFromURL(img).execute(URLIMAGE);
        }
    });

What you are trying to do, is probably getting value provided in the EditText. 您正在尝试做的事情,可能是在EditText中获得提供的价值。

First of all the structure will always give you null, as et_num is not declared yet, so it will be always null in below scenario. 首先,该结构将始终为您提供null,因为尚未声明et_num,因此在以下情况下它将始终为null。

EditText et_num;
String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num+".png";

Change the URLIMAGE to String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/" 将URLIMAGE更改为String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"

Then in OnClickListener get the actual value of EditText and append to URL 然后在OnClickListener中获取EditText的实际值并附加到URL

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = et_num.getText().toString();
            new GetImageFromURL(img).execute(URLIMAGE + text + ".png");
        }
    });

First of all, et_num is an Object(EditText) and not the text. 首先,et_num是一个Object(EditText),而不是文本。 During initialization your URLIMAGE initializes before you init your et_num, so it's null. 在初始化期间,URLIMAGE在初始化et_num之前会初始化,因此它为null。 Remove URLIMAGE at all and replace this: 完全删除URLIMAGE并替换为:

new GetImageFromURL(img).execute(URLIMAGE);

with this: 有了这个:

String url = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText()+".png";
new GetImageFromURL(img).execute(url);

put this inside the onClick() method of the button btn 将其放在按钮btnonClick()方法中

String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText().toString()+".png";

like this 像这样

btn.setOnclicklistener(new View.OnClickListener(){
           @Override
        public void onClick(View v) {   
            String URLIMAGE = "https://www.avansys.edu.pe/sites/default/files/"+et_num.getText().toString()+".png";

            new GetImageFromURL(img).execute(URLIMAGE);
      }
       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM