应用程序运行但setImageBitMap()不生成QR代码

问题描述 投票:0回答:1

我正在尝试做一个Android应用程序。它是生成QR码的基本应用程序。以前,只需使用“生成QR码”按钮,该应用就可以完美运行。但我决定将生成器QR图像放在由TabLayout控制的ViewPager中。

我能够配置2个选项卡,每个选项卡都有一个唯一的QR码。

这是每个Tab片段的内容:

 <TextView
        android:id="@+id/action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="35dp"
        android:text="@string/action_text"
        android:textFontWeight="bold"
        android:textSize="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/receive_QR"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_marginTop="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/action_text"
        app:srcCompat="@drawable/logo"
        tools:srcCompat="@drawable/logo" />

注意:第二片的图像视图有send_QR id

这是java文件的代码片段:

public class Tab1Fragment extends Fragment {

    private static final android.os.Environment Environment = null;
    String TAG = "GenerateQRCode";
    ImageView receiveQR;
    String savePath = Environment.getExternalStorageDirectory().getPath() + "/QRCode/";
    Bitmap bitmap;
    QRGEncoder receiveQrgEncoder;
    int smallerDimension;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_one, container, false);
        receiveQR = (ImageView) view.findViewById(R.id.receive_QR);

        smallerDimension = (100 * 3) / 5;

        receiveQrgEncoder = new QRGEncoder("QR LOGIC GOES HERE", null, QRGContents.Type.TEXT, smallerDimension);
        try{
            bitmap = receiveQrgEncoder.encodeAsBitmap();
            receiveQR.setImageBitmap(bitmap);
        }catch (WriterException e) {
            Log.v(TAG, e.toString());
        }

        return inflater.inflate(R.layout.fragment_one, container, false);

    }


}

我错过了什么? 非常感谢您在这件事上的时间和帮助。

java android android-layout
1个回答
0
投票

这只是因为你在onCreateView()的最后一行再次返回一个新的膨胀视图。因此,您的绑定视图将被重置,因此receiveQR将被清空。以下代码不正确:

public class Tab1Fragment extends Fragment {

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_one, container, false);
        receiveQR = (ImageView) view.findViewById(R.id.receive_QR);

        ...

        // you're returning a new inflated view here which is incorrect.
        return inflater.inflate(R.layout.fragment_one, container, false);

    }
}

您需要返回先前的膨胀视图,如下所示:

public class Tab1Fragment extends Fragment {

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.fragment_one, container, false);
        receiveQR = (ImageView) view.findViewById(R.id.receive_QR);

        ...

        return view;

    }


}
© www.soinside.com 2019 - 2024. All rights reserved.