【问题标题】:How to fix "android.content.ActivityNotFoundException" android-studio 2.3.3如何修复“android.content.ActivityNotFoundException”android-studio 2.3.3
【发布时间】:2019-02-11 15:57:42
【问题描述】:

我有一个小问题,当我在我的应用程序中单击某个按钮时,应用程序每次都会完全崩溃而不会失败。

我使用的是 android studio 2.3.3,该应用是条码扫描器,这是我收到的错误消息:

android.content.ActivityNotFoundException:找不到要处理的活动 意图 { act=android.intent.action.VIEW dat=5010029217902 }

这是导致错误的代码部分:

    }
    });
    builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
            startActivity(browserIntent);
        }
    });
    builder.setMessage(result.getText());
    AlertDialog alert1 = builder.create();
    alert1.show();

【问题讨论】:

  • 哪里出错了?
  • 嘿丹尼尔,当我点击应用程序中的“访问”按钮时出现错误。它应该从条形码扫描中获取信息并将其放入互联网搜索中,因此“访问”。当我点击这个按钮时,上面的错误信息就是我得到的......希望这能回答你的问题,如果没有,让我知道我可以给你什么其他细节
  • 我的意思是,在代码的哪一行?
  • 加上什么是myResult?它的价值在哪里?
  • 你能发布 myResult 的值吗?我猜这似乎不是一个 URL

标签: android-studio activitynotfoundexception


【解决方案1】:

扫描条形码时的结果通常不是有效的 URL。它通常只是一个带有几个数字的字符串。它没有 schemaprotocol,因此(很可能)没有定义“它是什么类型的资源定位器”。 Android 的 ACTION_VIEW 意图主要是使用此信息来决定“启动哪个应用程序/活动来打开此 URL”。由于缺少重要信息,Android 无法打开它。

您可以指定一些情况来处理结果。比如结果以“http://”或“https://”开头,直接用你的代码来处理,但如果只是一串数字,直接显示,或者在前面的某个字符串后面追加它用于Uri.parse,例如“https://google.com/search?q=”,以根据您的需要搜索此条形码的值,或您想要对 13 位结果执行的其他操作。

例如(下面用手机写的代码没有经过测试,只是展示思路):

@Override 
public void onClick(DialogInterface dialog, int which) {
    Intent browserIntent;
    if (myResult.startsWith("http://") || myResult.startsWith("https://"))
        browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
    else
        browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com/search?q=" + myResult)); 
    startActivity(browserIntent); 
}

【讨论】:

  • 是的!这正是我想要做的......一个包含从条形码返回的数字的谷歌搜索。你介意告诉我怎么做吗?我很新,我不知道该怎么做。非常感谢!
  • @MohNab 查看我的更新答案。这比您想象的要容易。
  • 我已将此添加到我的代码中,它绝对完美!非常感谢您的耐心、帮助和快速回复! :D
  • @MohNab 如果某人的回答成功地帮助了您,您可以投票并接受该回答,这有助于其他人更快地解决他们的类似问题,例如我的 :-)
  • 我点了绿色的勾,是这样接受答案的吗?
【解决方案2】:
package com.example.priyanka.qrbarcodescanner;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.content.ClipboardManager;


import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

import static android.Manifest.permission.CAMERA;

public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

    private static final int REQUEST_CAMERA = 1;
    private ZXingScannerView scannerView;
    private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        scannerView = new ZXingScannerView(this);
        setContentView(scannerView);
        int currentApiVersion = Build.VERSION.SDK_INT;

        if(currentApiVersion >=  Build.VERSION_CODES.M)
        {
            if(checkPermission())
            {
                Toast.makeText(getApplicationContext(), "Permission already granted!", Toast.LENGTH_LONG).show();
            }
            else
            {
                requestPermission();
            }
        }
    }

    private boolean checkPermission()
    {
        return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED);
    }

    private void requestPermission()
    {
        ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
    }

    @Override
    public void onResume() {
        super.onResume();

        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
            if (checkPermission()) {
                if(scannerView == null) {
                    scannerView = new ZXingScannerView(this);
                    setContentView(scannerView);
                }
                scannerView.setResultHandler(this);
                scannerView.startCamera();
            } else {
                requestPermission();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        scannerView.stopCamera();
    }

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA:
                if (grantResults.length > 0) {

                    boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                    if (cameraAccepted){
                        Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
                    }else {
                        Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (shouldShowRequestPermissionRationale(CAMERA)) {
                                showMessageOKCancel("You need to allow access to both the permissions",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                                    requestPermissions(new String[]{CAMERA},
                                                            REQUEST_CAMERA);
                                                }
                                            }
                                        });
                                return;
                            }
                        }
                    }
                }
                break;
        }
    }

    private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
        new android.support.v7.app.AlertDialog.Builder(MainActivity.this)
                .setMessage(message)
                .setPositiveButton("OK", okListener)
                .setNegativeButton("Cancel", null)
                .create()
                .show();
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void handleResult(Result result) {
        Log.d("QRCodeScanner", result.getText());
        Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
        final static String myResult = result.getText();


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Scan Result");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                scannerView.resumeCameraPreview(MainActivity.this);

            }
        });
        builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
                startActivity(browserIntent);
            }
        });
        builder.setMessage(result.getText());
        AlertDialog alert1 = builder.create();
        alert1.show();
    }
}

【讨论】:

    【解决方案3】:
    package com.example.priyanka.qrbarcodescanner;
    
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.net.Uri;
    import android.os.Build;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    import android.content.ClipboardManager;
    
    
    import com.google.zxing.Result;
    
    import me.dm7.barcodescanner.zxing.ZXingScannerView;
    
    import static android.Manifest.permission.CAMERA;
    
    public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    
        private static String myResult;
        private static final int REQUEST_CAMERA = 1;
        private ZXingScannerView scannerView;
        private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            scannerView = new ZXingScannerView(this);
            setContentView(scannerView);
            int currentApiVersion = Build.VERSION.SDK_INT;
    
            if(currentApiVersion >=  Build.VERSION_CODES.M)
            {
                if(checkPermission())
                {
                    Toast.makeText(getApplicationContext(), "Permission already granted!", Toast.LENGTH_LONG).show();
                }
                else
                {
                    requestPermission();
                }
            }
        }
    
        private boolean checkPermission()
        {
            return (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED);
        }
    
        private void requestPermission()
        {
            ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
        }
    
        @Override
        public void onResume() {
            super.onResume();
    
            int currentapiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
                if (checkPermission()) {
                    if(scannerView == null) {
                        scannerView = new ZXingScannerView(this);
                        setContentView(scannerView);
                    }
                    scannerView.setResultHandler(this);
                    scannerView.startCamera();
                } else {
                    requestPermission();
                }
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            scannerView.stopCamera();
        }
    
        public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
            switch (requestCode) {
                case REQUEST_CAMERA:
                    if (grantResults.length > 0) {
    
                        boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                        if (cameraAccepted){
                            Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
                        }else {
                            Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                if (shouldShowRequestPermissionRationale(CAMERA)) {
                                    showMessageOKCancel("You need to allow access to both the permissions",
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                                        requestPermissions(new String[]{CAMERA},
                                                                REQUEST_CAMERA);
                                                    }
                                                }
                                            });
                                    return;
                                }
                            }
                        }
                    }
                    break;
            }
        }
    
        private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
            new android.support.v7.app.AlertDialog.Builder(MainActivity.this)
                    .setMessage(message)
                    .setPositiveButton("OK", okListener)
                    .setNegativeButton("Cancel", null)
                    .create()
                    .show();
        }
    
        @Override
        public void onPointerCaptureChanged(boolean hasCapture) {
    
        }
    
        @Override
        public void handleResult(Result result) {
            Log.d("QRCodeScanner", result.getText());
            Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
            myResult = result.getText();
    
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Scan Result");
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    scannerView.resumeCameraPreview(MainActivity.this);
    
                }
            });
            builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
                    startActivity(browserIntent);
                }
            });
            builder.setMessage(result.getText());
            AlertDialog alert1 = builder.create();
            alert1.show();
        }
    }
    

    【讨论】:

    • 尝试将其复制粘贴到您的代码中。如果它不起作用,那么问题不在于 URI。
    • 嘿,我尝试将其粘贴到我的代码中,并且“myResult = result.getText ();”给我一个错误。它说:不兼容的类型。必需:字符串找到:java.lang.String
    • 对不起,艾迪。立即尝试。
    • 我只是想告诉你,如果二维码指向一个 URL 链接,例如 www.youtube.com,“访问”按钮可以正常工作,但是如果我正在扫描返回的条码一个 13 位数字作为值,当我单击“访问”时程序崩溃。这有意义吗?可以修复吗?
    • 您早该说过,如果代码正常工作,您可以保持原样。尝试添加trycatch。阅读更多here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2020-12-19
    相关资源
    最近更新 更多