我正在尝试做一个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);
}
}
我想念什么?
非常感谢您的时间和协助。
答案 0 :(得分:0)
这仅仅是因为您在C:\mingw32\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.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);
}
}