Android-应用程序意外停止错误且未启动

Android-应用程序意外停止错误且未启动,android,intellij-idea,Android,Intellij Idea,当我尝试在模拟器或物理设备上运行此程序时,应用程序不会启动,并且会出现一条消息,说明应用程序意外停止。只有一节课和一项活动。我已在清单中添加了所需的权限。我用的是iTellijIDE 这是密码 import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import

当我尝试在模拟器或物理设备上运行此程序时,应用程序不会启动,并且会出现一条消息,说明应用程序意外停止。只有一节课和一项活动。我已在清单中添加了所需的权限。我用的是iTellijIDE

这是密码

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);

    }

}
导入android.app.Activity;
导入android.app.Dialog;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.Environment;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入java.io.*;
导入java.net.URL;
导入java.net.URLConnection;
公共类MyActivity扩展了活动{
按钮下载,查看;
静态编辑文本uID,sID;
//进度对话框
私人对话;
公共静态最终整数进度条类型=0;
静态字符串值_sID=sID.getText().toString().trim();
静态字符串值_uID=uID.getText().toString().trim();
私有静态字符串文件\u url=”http://someurl.com“+”/“+value_sID+”/“+value_uID+”.pdf“+”/”;
字符串文件名=值\u sID+“\u”+值\u uID;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
下载=(按钮)findViewById(R.id.btn_下载);
视图=(按钮)findViewById(R.id.btn_视图);
uID=(EditText)findViewById(R.id.et_uID);
sID=(EditText)findViewById(R.id.et_sID);
下载.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建DownloadFileFromURL().execute(文件url);
}
});
}
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型://我们将其设置为0
pDialog=新建进度对话框(此对话框);
setMessage(“正在下载文件,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
违约:
返回null;
}
}
类DownloadFileFromURL扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//这将非常有用,以便您可以显示一个典型的0-100%
//进度条
int lenghtOfFile=conconnect.getContentLength();
//下载该文件
InputStream输入=新的BufferedInputStream(url.openStream(),
8192);
//输出流
OutputStream输出=新文件OutputStream(环境
.getExternalStorageDirectory().toString()
+“/dirs/”+文件名);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
解雇对话框(进度条类型);
}
}

}

您正试图从编辑文本中获取文本。 但是你在错误的地方做这件事

您需要在单击按钮时使用EditText的getText()

另外,在初始化之前不要从UI元素中获取数据。您正在onCreate()外部获取数据

上述行应位于以下内容之后:

sID = (EditText)findViewById(R.id.et_sID);

请阅读安卓文档。

Put log plz以便更好地理解如何向我们展示您的日志。现在,当我输入字段并单击下载时,进度会在一小段时间内开始和结束。当我去文件管理器查看文件夹时,文件不在那里。如何解决这个问题?@Sanchit,这个问题在你的AsyncTask的doInBackground()中。我建议您从Internet上提供的示例中了解文件处理和权限。
sID = (EditText)findViewById(R.id.et_sID);