Ich versuche, einen Imageview in einem Fragmente zu setzen:Mit Imagview in Fragmente
ImageView imageView = (ImageView) getActivity().findViewById(R.id.qrCode);
try {
Bitmap bitmap = encodeAsBitmap(STR);
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
aber ich habe den Fehler
„Versuch, virtuelle Methode‚Leere android.widget.ImageView aufzuzurufen .setImageBitmap (android.graphics.Bitmap) 'auf einer Nullobjektreferenz ".
Dies funktioniert, wenn ich es in einer Klasse, die AppCompatActivity aber nicht in meiner Fragment-Klasse erweitert.
Die entsprechende XML-Datei ist:
<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">
<!-- tools:context="${relativePackage}.${activityClass}" > -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="20dp" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/lbl_name"
android:textSize="24dp" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13dp" />
<Button
android:id="@+id/btnLogout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@color/btn_logut_bg"
android:text="@string/btn_logout"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15dp" />
<ImageView
android:id="@+id/qrCode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</LinearLayout>
</RelativeLayout>
ist der Imageview zum Fragmentlayout? – Raghunandan