【发布时间】:2018-11-06 12:38:04
【问题描述】:
我正在制作一个应用程序,它基本上允许用户在编辑文本中输入 URL 和标题,然后通过按下下载按钮,图像将显示在另一个活动上。还有一些其他功能目前不相关。我遇到的问题是,当我输入应该显示下载图像的图像视图的 id 并按下载图像时,应用程序崩溃。到目前为止,这是我的代码:
主要活动:
package com.example.faraz.assignment2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void dl(View view) {
Intent intent = new Intent(this,DownloadActivity.class);
startActivity(intent);
}
public void viewA(View view) {
Intent viewPic = new Intent(this,ViewActivity.class);
startActivity(viewPic);
}
}
主要 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="dl"/>
<Button
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
android:onClick="viewA"/>
<Button
android:id="@+id/range"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Range" />
</android.widget.LinearLayout>
ViewActivity 是我试图显示图像的地方:
package com.example.faraz.assignment2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
}
}
ViewActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".DownloadActivity">
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars[0]" />
</android.widget.LinearLayout>
DownloadActivity 是下载图像的位置:
package com.example.faraz.assignment2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends AppCompatActivity {
private Button btn;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
btn = (Button)findViewById(R.id.buttonD);
et = (EditText)findViewById(R.id.link);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
URL url = new URL(et.getText().toString());
new MyDownloadTask().execute(url);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private class MyDownloadTask extends AsyncTask<URL, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(URL... params) {
URL url = params[0];
Bitmap bitmap = null;
try {
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bitmap = BitmapFactory.decodeStream(bis);
bis.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
ImageView myImage = (ImageView) findViewById(R.id.test);//error is here when I change id for imageview to the one in ViewActivity
myImage.setImageBitmap(bitmap);
} else {
Toast.makeText(getApplicationContext(), "Failed to Download
Image", Toast.LENGTH_LONG).show();
}
}
}
}
下载活动的代码取自here
下载活动 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linear_layout"
android:gravity="top|center_vertical|center_horizontal|center"
android:orientation="vertical"
tools:context=".DownloadActivity">
<EditText
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="URL"
android:inputType="textPersonName" />
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName" />
<Button
android:id="@+id/buttonD"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download" />
<ImageView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:srcCompat="@tools:sample/avatars" />
</android.widget.LinearLayout>
我将不胜感激。
编辑: 这是我现在的 DownloadActivity,但它仍然不会在 ViewActivity 上显示图片:
btn = (Button)findViewById(R.id.buttonD);
et = (EditText)findViewById(R.id.link);
pic = (ImageView) findViewById(R.id.pic);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String url = et.getText().toString();
Glide.with(DownloadActivity.this).load(url).into(pic);
} catch (Exception e) {
e.printStackTrace();
}
}
});
【问题讨论】:
-
请附上错误日志
-
欢迎来到stack-overflow先生,请访问stackoverflow.com/help/how-to-ask
-
您可以在 AsyncTask 之外执行
findViewById(R.id.test);,但您确实应该使用 Picasso 或 Glide 库从外部来源下载图像 -
你必须使用intent首先去查看activty,然后通过手机上的路径访问下载的文件来显示图像,当你没有给父级充气时,你不能使用findviewbyid查看