【问题标题】:App crashes when starting a activity启动活动时应用程序崩溃
【发布时间】:2017-02-19 18:29:03
【问题描述】:

我有一个 webview 应用程序,并且想在 url 中存在特定文本时从内部更新应用程序。

我在 webview 的 shouldOverrideUrlLoading 函数中调用这些:

Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;

这里是 Update.class

public class Update extends MainActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
        String fileName = "app-file.apk";
        destination += fileName;
        final Uri uri = Uri.parse("file://" + destination);

        File file = new File(destination);
        if (file.exists())
            file.delete();

        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(getIntent().getStringExtra("http://example.com/app-file.apk")));
        request.setDestinationUri(uri);
        dm.enqueue(request);

        final String finalDestination = destination;
        final BroadcastReceiver onComplete = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(finalDestination));
                    Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
                    openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    openFileIntent.setData(contentUri);
                    startActivity(openFileIntent);
                    unregisterReceiver(this);
                    finish();
                } else {
                    Intent install = new Intent(Intent.ACTION_VIEW);
                    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    install.setDataAndType(uri, "application/vnd.android.package-archive");
                    startActivity(install);
                    unregisterReceiver(this);
                    finish();
                }
            }
        };
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}

当我转到某个 url 启动活动时,应用程序崩溃。我很想看到任何解决方案。谢谢!!

我解决了,是权限错误。

【问题讨论】:

  • 任何崩溃报告?
  • 使用adb logcat 评估堆栈跟踪。
  • 我的钱在NullPointerException。当有人在 onCreate 方法中发布问题时,它总是 NullPointerException
  • 我从未尝试过 adb,但我现在才尝试过,而且不会停止。
  • 看这个http://pastebin.com/raw/Zhgk66UD

标签: java android webview


【解决方案1】:

我建议您查看 ADB Logcat,这将帮助您解决问题。 否则,请检查崩溃日志或确保您已在 manifest.xml 中声明了更新活动

【讨论】:

  • 如何查看崩溃日志?我是新手,所以请指导我。此外,在 mainifest.xml 中声明了活动
【解决方案2】:

确保在 Android Manifest.xml 中声明新活动

尝试改变这个

      Intent intent = new Intent(getApplicationContext(), Update.class);
      startActivity(intent);
      return true;

到这里

 Intent intent = new Intent(getApplicationContext(), Update.class);
 startActivity(intent);
 finish();
 return true;

让我知道这是怎么回事。

【讨论】:

  • 是的,我在 Mainifest.xml 中添加了活动,结果是一样的。添加代码后应用程序仍然崩溃:(
  • 检查您的应用程序日志并从中发布代码...应用程序日志是您运行应用程序时在屏幕底部弹出的小框
  • 我没有在应用程序中运行模拟器。将其安装在手机中,因此不确定该盒子是如何出现的。你能说出来吗??
  • 在您运行应用程序时让您的手机插上电源,请问您是否使用 Android Studio?
  • 是的,我正在使用 android studio,但我仍然不明白你对 plug 的意思。如果你问的是 Logcat,我可以发给你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 2015-09-20
  • 2013-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多