我正在创建一个应用程序,它从条形码中读取初始任务。下面是代码块和生成的错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity {com.example.knr/com.example.knr.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.knr.MainActivity.onActivityResult(MainActivity.java:84)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
一切正常,当我点击按钮打开条形码扫描仪时,但是一旦我完成扫描,我就会收到此错误。我试图启动一个else,这样我就不会得到一个null异常,但它仍然保持不变。
主要片段的代码块
public class MainFragment extends Fragment {
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button) view.findViewById(R.id.button);
Button scan1 = (Button) view.findViewById(R.id.Scan);
final EditText et = (EditText) view.findViewById(R.id.editText); // also serves //toinput code scanned from barcode scanner
scan1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Intent intent = new Intent("com.google.zxing.client.android.SCAN");
// getActivity().startActivityForResult(intent, 0);
IntentIntegrator scanIntegrator = new IntentIntegrator(getActivity());
scanIntegrator.initiateScan();
}
});
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// do some stuff for another button click and check for conditions
}
});
return view
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
//retrieve scan result
//super.onActivityResult(requestCode, resultCode, intent);
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
//we have a result
String scanContent = scanningResult.getContents();
//contentTxt.setText("CONTENT: " + scanContent);
Toast toast = Toast.makeText(getActivity(),
scanContent, Toast.LENGTH_SHORT);
toast.show();
//et1.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getActivity(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
答案 0 :(得分:2)
您正在使用片段,而您在onCreate中使用EditText而不是片段的onCreateView。稍后在onActivityResult中调用et1.setText(barcont);在那里你得到null。
尝试:
public static class MainFragment extends Fragment {
EditText et1;
public MainFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_display_grup,
container, false);
et1 = (EditText) rootView.findViewById(R.id.editText);
return rootView;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
barcont=contents;
Log.d(TAG,barcont);
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.d(TAG,format);
barfor=format;
}
else{
Toast toast = Toast.makeText(getActivity(),
"No data received!", Toast.LENGTH_SHORT);
toast.show();}
}
et1.setText(barcont);
}
}
另外,et1应该是Fragment中的全局变量,而不是活动,你应该在片段中实现onActivityResult。
答案 1 :(得分:0)
您未正确初始化编辑文字:
final EditText et1 = (EditText) findViewById(R.id.editText);
应该是:
et1 = (EditText) findViewById(R.id.editText);
因为et1是一个成员变量。
答案 2 :(得分:0)
解决,
调用活动以在MainFragment外部写入EditText的备用或环形路径是在MainActivity中声明一个变量,并通过仅将值传递给Fragment中的EditText来赋值。
示例说你需要的字符串或变量是
String X1= "output of barcode scanner"
然后
import com.example.yourpackaganame.yourclassname
进入Fragment类并通过
调用它et.setText(yourclassname.X1)
对我来说工作得很好!!