我创建了一个应用程序,通过单击列表项,在点击按钮时从Internet
下载图像(同时它将url
放在EditText
上用户查看url
是什么。
最初我在隐藏模式下设置ProgressBar
和TextView
(表示Loading....
)。但是当下载开始时,我想将TextView
和ProgressBar
同时打开到UI上,一旦下载完成,就想让它们都不可见。
要发布的LogCat
上没有错误消息。我相信我错过了一些棘手的东西:)。如果需要任何其他信息,请与我们联系。提前谢谢。
以下XML
:LinearLayout
应为ON/OFF
<LinearLayout
android:id="@+id/loadingSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/downloadImage"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="@+id/loadingMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Loading....."
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
</LinearLayout>
以下是Java
文件:
public class DownloadImages_HandlerMainActivity extends Activity implements OnItemClickListener {
private EditText editText;
private ListView listView;
private TextView textView;
private String[] listOfImages;
private Button downloadImage;
private ProgressBar progressBar;
private LinearLayout loadingSection = null;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.downloadimages_main);
editText = (EditText) findViewById(R.id.downloadURL);
textView = (TextView) findViewById(R.id.loadingMessage);
listView = (ListView) findViewById(R.id.urlList);
downloadImage = (Button) findViewById(R.id.downloadImage);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
listOfImages = getResources().getStringArray(R.array.imageURLs);
listView.setOnItemClickListener(this);
handler = new Handler();
}
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
editText.setText(listOfImages[position]);
}
public void downloadImage(View view) {
String url = editText.getText().toString();
Thread myThread = new Thread(new DownloadImagesThread(url));
myThread.start();
}
public boolean downloadImageUsingThreads(String url) {
boolean successful = false;
URL downloadURL = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
File file = null;
FileOutputStream fileOutputStream = null;
try {
downloadURL = new URL(url);
connection = (HttpURLConnection) downloadURL.openConnection();
inputStream = connection.getInputStream();
file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath()
+ "/" + Uri.parse(url).getLastPathSegment());
fileOutputStream = new FileOutputStream(file);
int read = -1;
byte[] buffer = new byte[1024];
while ((read = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, read);
//Log.d("BRK0018", " " + read);
successful = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.d("BRK0018", " " + e);
} catch (IOException e) {
e.printStackTrace();
Log.d("BRK0018", " " + e);
} finally {
// This is the HANDLER INSTANCE in place of thread
handler.post(new Runnable() {
@Override
public void run() {
try {
loadingSection.setVisibility(View.GONE); // Making the ProgressBar INVISIBLE
} catch (Exception e) {
Log.d("BRK0018", " " + e);
}
}
});
if (connection != null) {
connection.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("BRK0018", " " + e);
}
}
}
return successful;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class DownloadImagesThread implements Runnable {
private String url;
public DownloadImagesThread(String url) {
this.url = url;
}
// This is the HANDLER INSTANCE in place of thread
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
try {
// TODO Auto-generated method stub
loadingSection.setVisibility(View.VISIBLE); // Making the ProgressBar VISIBLE
} catch (Exception e) {
Log.d("BRK0018", " " + e);
}
}
});
downloadImageUsingThreads(url);
}
}
}
答案 0 :(得分:1)
此处的问题是loadingSection
null
。由于您在try/catch
块中更改了其可见性,因此该应用不会崩溃。在onCreate()
:
loadingSection = (LinearLayout) findViewById(R.id.loadingSection);
答案 1 :(得分:0)
首先,当你打电话
handler.post(new Runnable() {
@Override
public void run() {
try {
// TODO Auto-generated method stub
loadingSection.setVisibility(View.VISIBLE);
Thread.sleep(1000);
} catch (Exception e) {
Log.d("BRK0018", " " + e);
}
}
});
Handler.class运行在UI线程中的代码之上。
您不能在UI线程中调用Thread.sleep(1000)
。
(但无论如何要记住必须在UI线程中调用loadingSection.setVisibility(View.VISIBLE);
)
修复它。是否有帮助写信给我。