Hinzufügen eines textview dynamisch im fragment

Ich bin neu in der android Entwicklung ... ich bin das arbeiten mit fragment.Das problem bin ich gerade konfrontiert ist-kann ich nicht hinzufügen, anzeigen dynamisch im fragment.Wenn ich das versuchte bekam ich eine nullpointerexception...Mein code-snippet wie folgt:

final LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.connections_layout, container, false);
EditText editText = new EditText(getActivity());

final int i = 5;
editText.setId(i); //Set id to remove in the future.
editText.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
editText.setText("Hello");
Log.d("View","Start");
try{
    linearLayout.addView(editText);
}catch(Exception e){
    e.printStackTrace();
}

Den connection_layout.xml - Datei ist wie folgt:

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/connections"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:background="#FF0000" >

  </LinearLayout>

Das fragment ist ein Mitglied einer tabgroup, gehört zum fragment-Aktivität.Was mache ich falsch in meinem code-snippet? Jede Art von Hilfe ist willkommen.Vielen Dank im Voraus...

Ich habe versucht zu finden das linearlayout mit seiner id..Aber dieser Ansatz war ebenfalls erfolglos...
wo ist dieser code?
wo bekommt man NPE? (genau dieser Zeile)
die Linie, die innerhalb des try-Blocks generiert die NPE..
geteilt haben die codes mit meinen Fragen..es gibt nichts mehr ich sollte für ein besseres Verständnis z.B. logcat...??

InformationsquelleAutor SALEH | 2012-10-24

Schreibe einen Kommentar