2013-07-11 9 views
33

Se provo a gonfiare una vista all'interno di un frammento sto ottenendo NULL .. Per esempio:Come per gonfiare vista all'interno frammento

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Here I will inflate my view using the layout ID & return. 
    return view; 
} 

Ogni volta che un pulsante viene premuto Ho bisogno di creare una visione dinamica ad esempio: pulsante & aggiungere al LinearLayout. Vorrei eseguire questa operazione dentro la mia classe come questo frammento:

public void addPlaces() {  
    Button button = new Button(null); 
    button.setText("button name"); 
    // e.g. like adding button to enter code here linear layout 
    linearLayout.addView(button); 
} 

Quindi, se ho gonfiare LinearLayout all'interno onCreateView e utilizzarlo in classe aggiuntivo, sto diventando NULL. Come raggiungere?

+1

inizializzare LinearLayout in 'oncreateView'. Dichiara linearlayout come membro della classe e usalo in addPlaces. – Raghunandan

+0

Dove prendi il null ?? Inoltre, non usare 'new Button (null)', passa un 'Context' invece di' null'! – SimonSays

risposta

56

dichiarare la variabile come variabile di istanza e poi inizializzare layout lineare

LinearLayout linearLayout; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment1, container, false); 
    linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout); 
    return rootView; 
} 

Poi

public void addPlaces() { 
    Button button = new Button(getActivity()); 
    // needs activity context 
    // fragment hosted by a activity. use getActivity() to get the context of the hosting activity. 
    button.setText("button name"); 
    linearlayout.addView(button); 
} 

Esempio: Modificare il seguito secondo la vostra esigenza.

fragment1.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Button" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:id="@+id/linearlayout" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:orientation="vertical" > 
    </LinearLayout> 

</RelativeLayout> 

Myfragment.java

public class Myfragment extends Fragment { 

    LinearLayout linearLayout; 
    View rootView; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     Button b = (Button) rootView.findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       addPlaces(); 
      } 

     }); 
     linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout); 
    } 

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

    public void addPlaces() { 
     Button button = new Button(getActivity()); // needs activity context 
     button.setText("button name"); 
     linearLayout.addView(button); 
    } 
} 

Snap Shot del mio emulatore

enter image description here

Modifica:

attività-main.xml

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <fragment android:name="com.example.fragments.Myfragment" 
      android:id="@+id/frag" 
      android:layout_above="@+id/button1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:text="Button" /> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends FragmentActivity { 
    Button b; 
    Myfragment fragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragment = new Myfragment(); 
     fragmentTransaction.add(R.id.frag, fragment); 
     fragmentTransaction.commit(); 
     b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       fragment.addPlaces(); 
      } 

     }); 
    } 
} 

Myfragment.java

public class Myfragment extends Fragment { 

    LinearLayout linearLayout; 
    View rootView; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout); 
    } 

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

    public void addPlaces() { 
     Button button = new Button(getActivity()); // needs activity context 
     button.setText("button name"); 
     linearLayout.addView(button); 
    } 
} 
+0

Ciao, se chiamo GRPlacesFragment grplaces = new GRPlacesFragment(); getSupportFragmentManager(). beginTransaction(). add (R.id.myresqplaces, grplaces). com(); grplaces.addPlaces (questo); se faccio così, il mio controllo non colpisce affatto il metodo oncreateview. – Naruto

+0

@ll non è necessario passare 'this'' getActivity() 'è sufficiente. – Raghunandan

+0

Definitivamente, sto chiamando da frammentactivity, quindi proverò ora il tuo codice – Naruto

Problemi correlati