安卓 - 条形码片段结果不显示



[已解决]这些应用程序工作正常而不会崩溃,但它应该将结果视图文本从"Hasil扫描"更新到扫描结果,但它没有。问题是文本视图(结果视图(在扫描后未更新。我使用的是DM77智兴条码扫描器。这是我到目前为止完成的代码。

主要活动:

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();
    }
}

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

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();
    }
}

我认为问题是onActivityResult没有在ScanFragment中调用

首先,您应该覆盖onActivityResult(int rc,int res, Intent data)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);
    }
}

这段代码将帮助您调用ScanFragment上的onActivityResult

最新更新