为什么我的textview没有显示任何内容?

时间:2017-01-05 05:10:54

标签: java android barcode zxing barcode-scanner

我目前正在做一款带有条形码扫描仪的应用。目前,当我扫描条形码时,它应该显示条形码的格式和内容。但目前扫描后它没有显示格式和内容。

这是代码

public class ThreeFragment extends Fragment{

    public ThreeFragment() {
        // Required empty public constructor
    }

    Button scan_btn;
    EditText Edit_current;
    TextView formatTxt, contentTxt;

    @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);
        final Activity activity = getActivity();
        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            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;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result.getContents() != null) {
            if (result.getContents() != null){
                String scanContent = result.getContents();
                String scanFormat = result.getFormatName();

                // display it on screen
                formatTxt.setText("FORMAT: " + scanFormat);
                contentTxt.setText("CONTENT: " + scanContent);
            } 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="@string/scan" />

    <TextView
        android:id="@+id/scanFormat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/scan_button" />
    <TextView
        android:id="@+id/scanContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textIsSelectable="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/scanFormat" />
</RelativeLayout>

我已将代码编辑到此

@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);
    formatTxt = (TextView) view.findViewById(R.id.scanFormat);
    contentTxt = (TextView) view.findViewById(R.id.scanContent);
    final Activity activity = getActivity();
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        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;
}

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


    }else{

            String scanContent = result.getContents();
            String scanFormat = result.getFormatName();

            // display it on screen
            formatTxt.setText("FORMAT: " + scanFormat);
            contentTxt.setText("CONTENT: " + scanContent);
    }
}
}}

仍然无法显示格式和内容

3 个答案:

答案 0 :(得分:0)

初始化您的formatTxt&amp;您onCreateView() method中的contentTxt! 扫描没问题,它只是因为没有初始化TextView

而给出了NPE
scan_btn = (Button) view.findViewById(R.id.scan_button);
formatTxt = (TextView) view.findViewById(R.id.scanFormat);
contentTxt = (TextView) view.findViewById(R.id.scanContent);

并在您的activityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result.getContents() != null) {
            String scanContent = result.getContents();
            String scanFormat = result.getFormatName();

            // display it on screen
            formatTxt.setText("FORMAT: " + scanFormat);
            contentTxt.setText("CONTENT: " + scanContent);
        }
    }

更新:这就是它在代码中的样子。

public class ThreeFragment extends Fragment{

    public ThreeFragment() {
        // Required empty public constructor
    }

    Button scan_btn;
    EditText Edit_current;
    TextView formatTxt, contentTxt;

    @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);
    //initialize the textViews 
    formatTxt = (TextView) view.findViewById(R.id.scanFormat);
    contentTxt = (TextView) view.findViewById(R.id.scanContent);
        final Activity activity = getActivity();
        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            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;
    }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
                IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                if (result.getContents() != null) {
                    String scanContent = result.getContents();
                    String scanFormat = result.getFormatName();

                    // display it on screen
                    formatTxt.setText("FORMAT: " + scanFormat);
                    contentTxt.setText("CONTENT: " + scanContent);
                }else {
                    Toast toast = Toast.makeText(getContext(),"No scan data received!", Toast.LENGTH_SHORT);
                    toast.show();
                }
            }

    }

答案 1 :(得分:0)

    package com.sp.ez_mart_xy;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

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_main);


        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.addFragment(new OneFragment(), "NAVIGATION");
        adapter.addFragment(new TwoFragment(), "HOME");
        adapter.addFragment(new ThreeFragment(), "PRICE-CHECKER");
        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 addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

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

这是我的主要活动,但我没有看到任何错误

答案 2 :(得分:-1)

您的代码至少存在两个问题:

  1. 您有两次相同的if语句:

    if (result.getContents() != null) {
        if (result.getContents() != null){
    

    外面的那个没有else,所以你不会知道条件何时失败,即使内部条件确实有else

  2. formatTxt未初始化。您需要在findViewById()的某处添加对onCreateView()的调用以对其进行初始化。