2016-07-20 7 views
0

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> 
+0

ist der Imageview zum Fragmentlayout? – Raghunandan

Antwort

0

Es sollte so sein.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.your_layout, container, 
      false); 

    ImageView imageView = (ImageView) rootView.findViewById(R.id.qrCode); 

    return rootView; 
} 
0

getActivity() Anstelle dieser Verwendung getView()

Wie

ImageView imageView = (ImageView) getView().findViewById(R.id.qrCode); 

in Ihrem Fragment

getActivity() Objekt die Aktivität wieder auf wen dein Fragment ich s attached, während getView() Ihnen das rootview Ihres Fragments zurückgibt, in dem alle anderen Ansichten vorhanden sind und Sie sie dann durch ihre Identifikation finden können.

0

Machen Sie es public static in Ihrer Aktivitätsklasse und verwenden Sie es in Ihrem Fragment wie folgt:

In Ihrer Tätigkeit;

public static ImageView imageView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.YOURLAYOUT); 


imageView = (ImageView) getActivity().findViewById(R.id.qrCode); 

} 

In Ihrem Fragment,

try { 
    Bitmap bitmap = encodeAsBitmap(STR); 
    YOURACITVITYNAME.imageView.setImageBitmap(bitmap); 
} catch (WriterException e) { 
    e.printStackTrace(); 
} 
0

Ich glaube, Sie es

falsch machen
ImageView imageView = (ImageView) getActivity().findViewById(R.id.qrCode); 

Dies ist nicht die Art und Weise Sie Fragment tun in class.In Fragment gibt es eine Ansicht (inflator.inflate), die durch das ein Layout n aufbläst Sie können auf alle IDs zugreifen. So sollten Sie etwas wie folgt versuchen:

ImagView imageView = (ImageView) view.findViewById(R.id.qrCode); 
0

Speichern Fragmentansicht in der Klasse Variable.

private View myFragmentView; 

In oncreateView

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     myFragmentView = inflater.inflate(R.layout.signup_addressinfo_layout, container, false); 

    return myFragmentView; 
} 

In Ihrer Funktion

ImageView imageView = (ImageView) myFragmentView.findViewById(R.id.qrCode); 
0

Die Art und Weise ist falsch machen, Sie immer die Null-Objekt Referenz-Fehler erhalten, wie im Fall des Fragments, sieht Bindung passiert in onCreateView()

Sie müssen es auf diese Weise tun:

public class MyFragment extends Fragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     // here replace fragment_layout with your xml layout that you have created 
     View v = inflater.inflate(R.layout.fragment_layout, container, false); 
     ImageView ivQrCode =(ImageView) v.findViewById(R.id.qrCode); 

     try { 
      Bitmap bitmap = encodeAsBitmap(STR); 
      ivQrCode.setImageBitmap(bitmap); 
     } catch (WriterException e) { 
      e.printStackTrace(); 
     } 

     return v; 
    } 
}