2017-07-13 1 views
0

J'ai mis en onClickListener mais à l'intérieur onCreateView() il est incapable de trouver l'id du bouton et à savoir « myButton »Je suis en train d'ajouter onClick dans fragment, mais il est incapable de trouver le bouton id

Mon code est:

public class Category extends Fragment implements View.OnClickListener { 

View view;Button b; 
public Category() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    view=inflater.inflate(R.layout.fragment_category, container,false); 
    b=(Button)view.findViewById(R.id.myButton); 
    b.setOnClickListener(this); 
    return view; 
} 

Mon fragment_category.xml est comme ci-dessous, il est contenant un bouton et un textView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="layout.Category" 
android:id="@+id/constraintLayout"> 

<TextView 
    android:layout_width="368dp" 
    android:layout_height="495dp" 
    android:text="@string/hello_blank_fragment" 
    tools:layout_editor_absoluteY="8dp" 
    tools:layout_editor_absoluteX="8dp" /> 

<Button 
    android:id="@+id/myButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_centerVertical="true" 
    android:elevation="0dp" 
    android:text="Button" 
    tools:layout_editor_absoluteX="135dp" 
    tools:layout_editor_absoluteY="208dp" /> 

+1

À quoi ressemble votre fragment xml? –

+0

ajoutez ici aussi votre importation de classe R –

+0

Monsieur J'ai ajouté le fichier xml s'il vous plaît vérifier @MuratK. – user8027365

Répondre

1

Vous êtes en train de gonfler "fragment_category", mais vous avez dit que vous avez posté "fragment" code XML.

Vous ne risquez pas de gonfler le bon fichier de disposition.

0

En fait, le problème était présent dans l'EDI après quelques rectifications, l'erreur est résolue.