嗨,我遇到了这个问题,因为我调试了行alrd
,因为我的行没有被调用@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();
}
}
}
onactivityresult没有被告知如何纠正这个?
答案 0 :(得分:0)
继承完整代码
import android.app.Activity;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
/**
* Created by P1422160 on 3/1/2017.
*/
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.scan_format);
contentTxt = (TextView) view.findViewById(R.id.scan_content);
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();
}}}
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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".HomeActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scan"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:id="@+id/btn_scan_now" />
<TextView
android:id="@+id/scan_format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_below="@+id/btn_scan_now"
android:gravity="center_horizontal"
android:layout_marginBottom="@dimen/activity_vertical_margin" />
<TextView
android:id="@+id/scan_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_below="@+id/scan_format"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:gravity="center_horizontal" />
此活动假设扫描条形码并显示其格式和内容