TextView不显示结果

时间:2017-01-07 12:54:40

标签: android barcode zxing barcode-scanner

我在这个应用程序中扫描条形码并显示结果。目前TexView在扫描条形码后没有显示任何内容你们可以帮助我吗?

这里是MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment(), "ONE");
        adapter.addFrag(new TwoFragment(), "TWO");
        adapter.addFrag(new ThreeFragment(), "THREE");
        adapter.addFrag(new FourFragment(), "FOUR");
        adapter.addFrag(new FiveFragment(), "FIVE");
        adapter.addFrag(new SixFragment(), "SIX");
        adapter.addFrag(new SevenFragment(), "SEVEN");
        adapter.addFrag(new EightFragment(), "EIGHT");
        adapter.addFrag(new NineFragment(), "NINE");
        adapter.addFrag(new TenFragment(), "TEN");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

这是扫描程序代码

public class OneFragment extends Fragment{
    public OneFragment() {
        // Required empty public constructor
    }

    Button scan_btn;
    TextView barcode;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle   savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        scan_btn = (Button) view.findViewById(R.id.scan_button);
        //initialize the textViews
        barcode = (TextView)view.findViewById(R.id.barcodeResult);
        final Activity activity = getActivity();
        scan_btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(true);
                integrator.initiateScan();
            }
        });

        return view;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            barcode.setText(result.getContents());
        }else {
            Toast toast = Toast.makeText(getContext(),"No scan data received!", Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

和xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button android:id="@+id/scan_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="scan" />

    <TextView
        android:id="@+id/barcodeResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Start scanning by pressing the FAB!"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

确保OnActivityResult中的Activity正在呼叫super.OnActivityResult()。这应该确保它也被Fragment调用。

或..

尝试这个包装类:而不是IntentIntegrator

import android.content.Intent;
import android.support.v4.app.Fragment;

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

public final class FragmentIntentIntegrator extends IntentIntegrator {

    private final Fragment fragment;

    public FragmentIntentIntegrator(Fragment fragment) {
        super(fragment.getActivity());
        this.fragment = fragment;
    }

    @Override
    protected void startActivityForResult(Intent intent, int code) {
        fragment.startActivityForResult(intent, code);
    }
}

确保调用fragment onactivityresult ..

在你的OneFragment中称之为;

FragmentIntentIntegrator integrator = new FragmentIntentIntegrator (this);

...或

请参阅此http://androide-examples.blogspot.com/2013/11/zxing-onactivityresult-called-only-in.html

它显示了详细实现

答案 1 :(得分:0)

当从另一个活动回来时,系统可能会尝试恢复视图状态,实际上会覆盖你刚写的内容。

将结果保存到成员中并尝试覆盖片段中的onViewStateRestored

    @Override
    public void onViewStateRestored(Bundle savedInstanceState){
     super.onViewStateRestored(savedInstanceState);
     if( barcode != null ){
         barcode.setText(mResult);
     }
    }

答案 2 :(得分:0)

很抱歉迟到了,但那是因为我编译你的项目有很多错误,你也可以更轻松地在Github上传它

无论如何,您使用扫描仪的方式根本没有输入onActivityResult()方法,因此我将其重新创建为MainActivity class of the sample library 还有你自定义

这是你的代码,我已经测试了它确实有效,你只需要复制并粘贴它

package info.androidhive.materialtabs.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

import info.androidhive.materialtabs.R;


public class OneFragment extends Fragment{
public OneFragment() {
    // Required empty public constructor
}

private String toast;
Button scan_btn;
TextView barcode;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    scan_btn = (Button) view.findViewById(R.id.scan_button);
    barcode = (TextView)view.findViewById(R.id.barcodeResult);
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            scanFromFragment();
        }
    });
    return view;
}

public void scanFromFragment() {
    IntentIntegrator.forSupportFragment(this)
            .setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES)
            .setPrompt("Scan")
            .setCameraId(0)
            .setBeepEnabled(false)
            .setBarcodeImageEnabled(true)
            .initiateScan();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null) {
        if(result.getContents() == null) {
            toast = "Cancelled from fragment";
        } else {
            barcode.setText(result.getContents());
            toast = "Scanned from fragment: " + result.getContents();
        }

        // At this point we may or may not have a reference to the activity
        displayToast();
    }
}

private void displayToast() {
    if(getActivity() != null && toast != null) {
        Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
        toast = null;
    }
}
}

同时添加到barcodeResult布局fragment_one中的android:layout_below="@+id/scan_button",因为它位于扫描按钮后面