Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在片段中导入“gson”_Android_Gson_Fragment - Fatal编程技术网

Android 如何在片段中导入“gson”

Android 如何在片段中导入“gson”,android,gson,fragment,Android,Gson,Fragment,在activity类中我没有得到gson错误,但是在编写预告片时我得到了一个错误。如何解决此错误 public class BarcodeReaderFragment extends Fragment { @Override public void receiveDetections(Detector.Detections < Barcode > detections) { final SparseArray < Barcode > qrcode = detec

在activity类中我没有得到gson错误,但是在编写预告片时我得到了一个错误。如何解决此错误

public class BarcodeReaderFragment extends Fragment {

@Override
public void receiveDetections(Detector.Detections < Barcode > detections) {
    final SparseArray < Barcode > qrcode = detections.getDetectedItems();
    if (qrcode.size() != 0) {
        Vibrator vibrator = (Vibrator) getActivity().getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(100);
        Intent my = new Intent(getActivity(), ResultActivity.class);
        my.putExtra("type", qrcode.valueAt(0));
        Gson gson = new Gson();
        String json = gson.toJson(qrcode.valueAt(0));
        SharedPreferences sp = getActivity().getSharedPreferences("list", Context.MODE_PRIVATE);
        addhistory(sp, json);
        startActivity(my);
        getActivity().finish();
        barcodeDetector.release();
    }
}

您需要按照说明将Gson库包含在项目中,并按如下方式导入Gson类:import com.google.Gson.Gson将其直接放在包声明下。

请在应用程序gradle中使用gradle导入库

implementation 'com.google.code.gson:gson:2.8.5'

你能把那个错误贴出来吗?我把Gson添加到了库中。我以前试过这个。它是从activity类的库中导入的,但不在track中。我将Gson添加到库中。我以前试过这个。它是从activity类的库中导入的,但不是在track.import.com.google.gson.gson中,我是手工编写的。如果您将鼠标悬停在红色文本上,IDE将显示一条错误消息。上面写了什么?谢谢你的关注。我将gson添加回库中,问题得到了改善。