当我尝试在模拟器或物理设备上运行时,应用程序无法启动,并显示一条消息,指出应用程序意外停止。只有1个课程和1个活动。我在清单中添加了所需的权限。我正在使用itelliJ IDE。
这是代码
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class MyActivity extends Activity {
Button download , view;
static EditText uID , sID;
// Progress Dialog
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
static String value_sID = sID.getText().toString().trim();
static String value_uID = uID.getText().toString().trim();
private static String file_url = "http://someurl.com"+"/"+value_sID+"/"+value_uID+".pdf"+"/";
String fileName = value_sID+"_"+value_uID;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
download = (Button)findViewById(R.id.btn_Download);
view = (Button)findViewById(R.id.btn_View);
uID = (EditText)findViewById(R.id.et_uID);
sID = (EditText)findViewById(R.id.et_sID);
download.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFileFromURL().execute(file_url);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/dirs/"+fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
}
答案 0 :(得分:0)
您正在尝试从EditText中获取文本。 但你是在错误的地方做的。
您需要在点击按钮时使用EditText的getText()。
此外,在初始化之前,请勿从UI Elements获取数据。您正在onCreate()之外获取数据。
sID.getText().toString().trim()
以上行应该在以下之后:
sID = (EditText)findViewById(R.id.et_sID);
请阅读Android文档。