启动活动以下载新版本的应用时应用崩溃

时间:2018-04-10 18:39:02

标签: android android-activity

当我启动应用程序时,我使用以下功能启动DownloadNewVersion任务:

04-09 19:49:19.959 3484-3484/com.app.sipp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.app.sipp, PID: 3484 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.sipp/com.app.sipp.MainActivity}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.app.sipp/com.app.sipp.updater$DownloadNewVersion}; have you declared this activity in your AndroidManifest.xml? at 

但是当我启动我的应用程序时,它只会因以下错误而崩溃:

public class Updater extends AppCompatActivity {

    ProgressDialog bar;
    private static String TAG = "MainActivity";
    private int AppVersion = 1;

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

        TextView heading    = (TextView) findViewById(R.id.heading);
        Button   update_btn = (Button) findViewById(R.id.btn);

        heading.setText("App Version: " + AppVersion);

        update_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new DownloadNewVersion().execute();

            }
        });

    }

    class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            bar = new ProgressDialog(MainActivity.this);
            bar.setCancelable(false);

            bar.setMessage("Downloading...");

            bar.setIndeterminate(true);
            bar.setCanceledOnTouchOutside(false);
            bar.show();

        }

        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);

            bar.setIndeterminate(false);
            bar.setMax(100);
            bar.setProgress(progress[0]);
            String msg = "";
            if(progress[0]>99){

                msg="Finishing... ";

            }else {

                msg="Downloading... "+progress[0]+"%";
            }
            bar.setMessage(msg);

        }
        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            bar.dismiss();

            if(result){

            Toast.makeText(getApplicationContext(),"Update Done",
                    Toast.LENGTH_SHORT).show();

            }else{

            Toast.makeText(getApplicationContext(),"Error: Try Again",
                    Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        protected Boolean doInBackground(String... arg0) {
            Boolean flag = false;

            try {


                URL url = new URL("http://androidpala.com/tutorial/app-debug.apk");

                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.setDoOutput(true);
                c.connect();


                String PATH = Environment.getExternalStorageDirectory()+"/Download/";
                File file = new File(PATH);
                file.mkdirs();

                File outputFile = new File(file,"app-debug.apk");

                if(outputFile.exists()){
                    outputFile.delete();
                }

                FileOutputStream fos = new FileOutputStream(outputFile);
                InputStream is = c.getInputStream();

                int total_size = 1431692;//size of apk

                byte[] buffer = new byte[1024];
                int len1 = 0;
                int per = 0;
                int downloaded=0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                    downloaded +=len1;
                    per = (int) (downloaded * 100 / total_size);
                    publishProgress(per);
                }
                fos.close();
                is.close();

                OpenNewVersion(PATH);

                flag = true;
            } catch (Exception e) {
                Log.e(TAG, "Update Error: " + e.getMessage());
                flag = false;
            }
            return flag;

        }

    }

    void OpenNewVersion(String location) {

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(location + "app-debug.apk")),
                "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

}

这是班级&#34;更新者&#34;,我想运行DownloadNewVersion下载该应用的新版本。

@RequestMapping(method = RequestMethod.POST)
ResponseEntity<?> add(@PathVariable String userId, @RequestBody Bookmark input) {
    this.validateUser(userId);

    return this.accountRepository
            .findByUsername(userId)
            .map(account -> {
                Bookmark result = bookmarkRepository.save(new Bookmark(account,
                        input.getUri(), input.getDescription()));

                URI location = ServletUriComponentsBuilder
                    .fromCurrentRequest().path("/{id}")
                    .buildAndExpand(result.getId()).toUri();

                return ResponseEntity.created(location).build();
            })
            .orElse(ResponseEntity.noContent().build());

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您尝试执行以下代码: startActivity(new Intent(this, updater.DownloadNewVersion.class));

这意味着您要告诉您的应用程序启动一项新活动,即活动&#34; updater.DownloadNewVersion&#34;。但是,您的类DownloadNewVersion是ASyncTask。

您的应用程序崩溃时出现以下警告:Unable to find explicit activity class {com.app.sipp/com.app.sipp.updater$DownloadNewVersion}; have you declared this activity in your AndroidManifest.xml?,因为它无法在您的代码和您的清单文件中找到您的活动,因为它实际上不是活动。

如果您尝试打开更新程序活动,请改用以下内容:

startActivity(new Intent(this, Updater.class));