java - 如何解决此问题无法解析方法startResultActivity com.google.zxing.Result?

标签 java android

我尝试在 ResultActivity 类上打开结果,但不能是为什么?

**我尝试扫描图库中图像的二维码**

任何解决方案

这是代码

case R.id.scn_img:
            Intent pickIntent = new Intent(Intent.ACTION_PICK);
            pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(pickIntent, 111);


            break;

这是 onActivityResult 代码

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        //the case is because you might be handling multiple request codes here
        case 111:
            if(data == null || data.getData()==null) {
                Log.e("TAG", "The uri is null, probably the user cancelled the image selection process using the back button.");
                return;
            }
            Uri uri = data.getData();
            try
            {
                InputStream inputStream = getContentResolver().openInputStream(uri);
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                if (bitmap == null)
                {
                    Log.e("TAG", "uri is not a bitmap," + uri.toString());
                    return;
                }
                int width = bitmap.getWidth(), height = bitmap.getHeight();
                int[] pixels = new int[width * height];
                bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
                bitmap.recycle();
                bitmap = null;
                RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
                BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
                MultiFormatReader reader = new MultiFormatReader();
                try
                {
                    Result result = reader.decode(bBitmap);
                    //here i want to do the code but this not working
                    // here is the problem
                    //********************************

                   ResultActivity.startResultActivity(BaseActivity.this, result);

                   //********************************
                    // toast message it is work good

                    Toast.makeText(this, "The content of the QR image is: "
                            + result.getText(), Toast.LENGTH_SHORT).show();

                }
                catch (NotFoundException e)
                {
                    Log.e("TAG", "decode exception", e);
                }
            }
            catch (FileNotFoundException e)
            {
                Log.e("TAG", "can not open file" + uri.toString(), e);
            }
            break;
    }
}

这是我的 ResultActivity 类代码

public class ResultActivity extends AppCompatActivity {

private static final String HISTORY_DATA = "ResultActivity.HISTORY_DATA";
private static final int REQUEST_WRITE_PERMISSION = 200;
private static BarcodeResult barcodeResult = null;
private static HistoryItem historyItem = null;

private ResultViewModel viewModel;

private ResultFragment currentResultFragment;

public static void startResultActivity(@NonNull Context context, @NonNull BarcodeResult barcodeResult) {
    ResultActivity.barcodeResult = barcodeResult;
    ResultActivity.historyItem = null;
    Intent resultIntent = new Intent(context, ResultActivity.class);
    context.startActivity(resultIntent);
}

public static void startResultActivity(@NonNull Context context, @NonNull HistoryItem historyItem) {
    ResultActivity.barcodeResult = null;
    ResultActivity.historyItem = historyItem;
    Intent resultIntent = new Intent(context, ResultActivity.class);
    resultIntent.putExtra(HISTORY_DATA, true);
    context.startActivity(resultIntent);
}

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

    viewModel = ViewModelProviders.of(this).get(ResultViewModel.class);

    initStateIfNecessary(savedInstanceState);

    ActionBar ab = getSupportActionBar();
    if(ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
    }

    if(isFinishing()) {
        return;
    }

    loadFragment(viewModel.mParsedResult);
    displayGeneralData();
}

private void initStateIfNecessary(Bundle savedInstanceState) {
    boolean hasHistoryItem = getIntent().getBooleanExtra(HISTORY_DATA, false);

    if(savedInstanceState == null) {
        if(hasHistoryItem && historyItem != null) {
            viewModel.initFromHistoryItem(historyItem);
        } else if(barcodeResult != null) {
            viewModel.initFromScan(barcodeResult);
        } else {
            // no data to display -> exit
            Toast.makeText(this, R.string.activity_result_toast_error_cant_load, Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share,menu);
    getMenuInflater().inflate(R.menu.copy,menu);
    getMenuInflater().inflate(R.menu.save, menu);

    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(menu != null) {
        MenuItem saveMi = menu.findItem(R.id.save);
        if(saveMi != null) {
            saveMi.setVisible(!viewModel.mSavedToHistory);
        }
    }

    return true;
}


public void onClick(View view) {
    if (view.getId() == R.id.btnProceed) {
        if(currentResultFragment != null) {
            currentResultFragment.onProceedPressed(this);
        }
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.share:
            Intent sharingIntent= new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, viewModel.mParsedResult.getDisplayResult());
            startActivity(Intent.createChooser(sharingIntent,getString(R.string.share_via)));
            return true;

        case R.id.save:

            viewModel.saveHistoryItem(viewModel.currentHistoryItem);
            invalidateOptionsMenu();
            Toast.makeText(this, R.string.activity_result_toast_saved, Toast.LENGTH_SHORT).show();
            return true;

        case R.id.copy:
            ClipboardManager clipboardManager =(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
            ClipData clipData = ClipData.newPlainText("Text", viewModel.mParsedResult.getDisplayResult());
            clipboardManager.setPrimaryClip(clipData);
            Toast.makeText(getApplicationContext(), R.string.content_copied, Toast.LENGTH_SHORT).show();
            return true;

        case android.R.id.home:
            onBackPressed();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

private void loadFragment(@NonNull ParsedResult parsedResult) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    ResultFragment resultFragment;

    switch (parsedResult.getType()) {
        case ADDRESSBOOK:
            resultFragment = new ContactResultFragment();
            break;
        case EMAIL_ADDRESS:
            resultFragment = new EmailResultFragment();
            break;
        case PRODUCT:
            resultFragment = new ProductResultFragment();
            break;
        case URI:
            resultFragment = new URLResultFragment();
            break;
        case GEO:
            resultFragment = new GeoResultFragment();
            break;
        case TEL:
            resultFragment = new TelResultFragment();
            break;
        case SMS:
            resultFragment = new SMSResultFragment();
            break;
        case WIFI:
            resultFragment = new WifiResultFragment();
            break;
        case TEXT:
        default:
            resultFragment = new TextResultFragment();
            break;
    }

    currentResultFragment = resultFragment;

    resultFragment.putQRCode(parsedResult);

    ft.replace(R.id.activity_result_frame_layout, resultFragment);
    ft.commit();
}

}

这是错误消息

无法解析 startResultActivity com.google.zxing.Result 方法

我想用一个例子来回答

谢谢

最佳答案

ResultActivity 中没有名为 startResultActivity 的静态方法,该方法将 ContextResult 作为参数

BarcodeResult 与 Result 不同

您需要创建一个带有签名的方法 public static void startResultActivity(@NonNull Context context, @NonNull Result result) {

关于java - 如何解决此问题无法解析方法startResultActivity com.google.zxing.Result?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61583021/

相关文章:

android - 从android中的html5视频标签运行视频

android - android 如何为具有不同权限和 Intent 过滤器的同一广播接收器解析 AndroidManifest 中的多个条目?

android - 4.4.4 不在 Android SDK 管理器中

java - 使用 HttpCore 的网络服务器的 http 请求/响应流程

java - opengl es 2.0 填充顶点和索引缓冲区

java - Web Crawler 在电子邮件链接上被阻止

java - openjdk11 : Unsupported CipherSuite Exception

Android通过 Activity 中的 Intent 将可绘制的图像传递给第二个 Activity

java - 从jsp向Javascript传递参数

java - 使用 Webview 和 Httpclient 检查用户是否登录