Android - 条码片段结果不显示 [英] Android - Barcode Fragment result not displaying

查看:13

[已解决]应用程序可以正常工作而不会崩溃,但应该将 resultView 文本从"Hasil Scan"更新为扫描结果,但事实并非如此.问题是文本视图(resultView)在扫描后没有更新.我使用 DM77 Zxing 条码扫描器.这是我到目前为止所做的代码.

主要活动:

import android.content.Intent;导入 android.support.annotation.NonNull;导入 android.support.design.widget.BottomNavigationView;导入 android.support.v4.app.Fragment;导入 android.support.v4.app.FragmentTransaction;导入 android.support.v7.app.AppCompatActivity;导入 android.os.Bundle;导入 android.view.MenuItem;导入 sc.smklabor.rpl.labsscanner.sc.smklabor.rpl.labsscanner.fragments.ScanFragment;公共类 MainActivity 扩展 AppCompatActivity {@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {@覆盖public boolean onNavigationItemSelected(@NonNull MenuItem item) {Fragment selectedFragment = null;开关(item.getItemId()){案例 R.id.scannav:selectedFragment = ScanFragment.instance();休息;}FragmentTransaction 事务 = getSupportFragmentManager().beginTransaction();transaction.replace(R.id.frame_layout, selectedFragment);交易.提交();返回真;}});FragmentTransaction 事务 = getSupportFragmentManager().beginTransaction();transaction.replace(R.id.frame_layout, ScanFragment.instance());交易.提交();}}

这里是扫描仪片段,我只做了一个片段,其他片段只是关于和网站扫描片段:

import android.content.Intent;导入android.net.Uri;导入 android.os.Bundle;导入 android.support.v4.app.Fragment;导入 android.view.LayoutInflater;导入 android.view.View;导入 android.view.ViewGroup;导入 android.webkit.URLUtil;导入 android.widget.Button;导入 android.widget.TextView;导入 com.google.zxing.integration.android.IntentIntegrator;导入 com.google.zxing.integration.android.IntentResult;导入 sc.smklabor.rpl.labsscanner.R;/*** 由 Lenovo 于 21/03/2018 创建.*/公共类 ScanFragment 扩展 Fragment 实现 View.OnClickListener {私人 IntentIntegrator 扫描仪;私人按钮开始扫描,btnGoUrl;私有文本视图结果视图;私人字符串扫描结果;公共静态 ScanFragment 实例(){ScanFragment f = new ScanFragment();返回 f;}公共无效 onCreate(Bundle b) {super.onCreate(b);扫描仪 = 新 IntentIntegrator(this.getActivity()).forSupportFragment(this);}公共无效onActivityResult(int rc,int res,意图数据){IntentResult 结果 = IntentIntegrator.parseActivityResult(rc, res, data);如果(结果!=空){scanResult = result.getContents().toString();if (scanResult.contains("--L--:")) {String postId = scanResult.split("://")[1];btnGoUrl.setVisibility(View.INVISIBLE);resultView.setText("帖子ID:" + postId);} else if (URLUtil.isHttpUrl(scanResult) || isValidURL(scanResult)) {btnGoUrl.setVisibility(View.VISIBLE);resultView.setText(result.getContents().toString());} 别的 {btnGoUrl.setVisibility(View.INVISIBLE);resultView.setText(result.getContents().toString());}}}公共视图 onCreateView(LayoutInflater inf, ViewGroup con, Bundle b) {查看视图 = inf.inflate(R.layout.scanlayout, con, false);startScan = (Button)view.findViewById(R.id.button);btnGoUrl = (Button)view.findViewById(R.id.btnGoUrl);扫描仪 = 新的 IntentIntegrator(getActivity());startScan.setOnClickListener(this);resultView = (TextView)view.findViewById(R.id.textView);btnGoUrl.setOnClickListener(new Button.OnClickListener() {公共无效onClick(查看e){Uri URL = Uri.parse(scanResult);Intent i = new Intent(Intent.ACTION_VIEW,URL);开始活动(i);}});返回视图;}公共布尔 isValidURL(String s) {return (s.contains("http://") || s.contains("https://")) &&(s.contains(".com") || s.contains(".org") || s.contains(".net") || s.contains(".id") || s.contains(".sch.id") || s.contains(".me"));}公共无效onClick(查看e){scanner.setPrompt("Arahkan kamera kepada 条码 tepat pada kotak.");扫描仪.setOrientationLocked(false);扫描仪.initiateScan();}}
解决方案

我认为问题是 onActivityResult 没有在 ScanFragment

中调用

首先你应该覆盖onActivityResult(int rc,int res, Intent data)在 MainActivity

 @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);片段片段 = getSupportFragmentManager().findFragmentById(R.id.frame_layout);如果(片段!= null){fragment.onActivityResult(requestCode, resultCode, data);}}

此代码将帮助您调用 ScanFragment 上的 onActivityResult.

  • 原文

[SOLVED] The apps work fine without crash, but it should be update the resultView text from "Hasil Scan" to the scan result, but it doesnt. The problem are the textview (resultView) are not updated after scan. Im using DM77 Zxing barcode scanner. Here is the code i have done so far.

MainActivity:

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import sc.smklabor.rpl.labsscanner.sc.smklabor.rpl.labsscanner.fragments.ScanFragment;

public class MainActivity extends AppCompatActivity {


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

        BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.scannav:
                                selectedFragment = ScanFragment.instance();
                                break;
                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, ScanFragment.instance());
        transaction.commit();

    }
}

and here is for the scanner fragment, i only have done one fragment, other fragment just will be about and website ScanFragment:

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import sc.smklabor.rpl.labsscanner.R;

/**
 * Created by Lenovo on 21/03/2018.
 */

public class ScanFragment extends Fragment implements View.OnClickListener {

    private IntentIntegrator scanner;
    private Button startScan,btnGoUrl;
    private TextView resultView;
    private String scanResult;

    public static ScanFragment instance() {
        ScanFragment f = new ScanFragment();
        return f;
    }

    public void onCreate(Bundle b) {
        super.onCreate(b);
        scanner = new IntentIntegrator(this.getActivity()).forSupportFragment(this);
    }

    public void onActivityResult(int rc,int res, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(rc, res, data);
        if(result!=null) {
            scanResult = result.getContents().toString();
            if (scanResult.contains("--L--:")) {
                String postId = scanResult.split("://")[1];
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText("Post ID: " + postId);
            } else if (URLUtil.isHttpUrl(scanResult) || isValidURL(scanResult)) {
                btnGoUrl.setVisibility(View.VISIBLE);
                resultView.setText(result.getContents().toString());
            } else {
                btnGoUrl.setVisibility(View.INVISIBLE);
                resultView.setText(result.getContents().toString());
            }
        }
    }

    public View onCreateView(LayoutInflater inf, ViewGroup con, Bundle b) {
        View view = inf.inflate(R.layout.scanlayout, con, false);
        startScan = (Button)view.findViewById(R.id.button);
        btnGoUrl = (Button)view.findViewById(R.id.btnGoUrl);
        scanner = new IntentIntegrator(getActivity());
        startScan.setOnClickListener(this);
        resultView = (TextView)view.findViewById(R.id.textView);

        btnGoUrl.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View e) {
                Uri URL = Uri.parse(scanResult);
                Intent i = new Intent(Intent.ACTION_VIEW,URL);
                startActivity(i);
            }
        });
        return view;
    }
    public boolean isValidURL(String s) {
        return (s.contains("http://") || s.contains("https://")) && (s.contains(".com") || s.contains(".org") || s.contains(".net") || s.contains(".id") || s.contains(".sch.id") || s.contains(".me"));
    }

    public void onClick(View e) {
        scanner.setPrompt("Arahkan kamera kepada barcode tepat pada kotak.");
        scanner.setOrientationLocked(false);
        scanner.initiateScan();
    }
}
解决方案

I think the problem is the onActivityResult is not called in the ScanFragment

First you should override the onActivityResult(int rc,int res, Intent data) on the MainActivity

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}

This code it will help you call the onActivityResult on the ScanFragment.