2016-04-16 13 views
0

Dieser Code funktioniert in grundlegenden Aktivität, aber es ist nicht in Registerkarten Aktivität. Ich möchte den ListView mit benutzerdefinierter Ansicht verwenden. Common Code:Android ListAdapter: NullPointerException Ausnahme

list_row.xml: 

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


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView2" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Medium Text" 
     android:id="@+id/textView3" /> 
</LinearLayout> 

Es ist meine einfache Datenklasse, will ich es zeigen:

String nev; 
String szakma; 

public String getNev() { 
    return nev; 
} 

public void setNev(String nev) { 
    this.nev = nev; 
} 

public String getSzakma() { 
    return szakma; 
} 

public void setSzakma(String szakma) { 
    this.szakma = szakma; 
} 

public Adat(String nev, String szakma) { 
    this.nev = nev; 
    this.szakma = szakma; 
} 

Und meine Gewohnheit ListAdapter:

public class EgyeniAdapter extends BaseAdapter { 

     Context context; 
     ArrayList<Adat> adatok; 

     public EgyeniAdapter(Context context, ArrayList<Adat> adatok) { 
      this.context = context; 
      this.adatok = adatok; 
     } 

     @Override 
     public int getCount() { 
      return adatok.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return adatok.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.list_row,null); // itt rendelem hozza a saját viewet 

      if (convertView != null) 
      { 
       TextView Cim = (TextView) convertView.findViewById(R.id.textView2); 
       TextView Alcim = (TextView) convertView.findViewById(R.id.textView3); 

       Adat adatom = adatok.get(position); 
       Cim.setText(adatom.getNev()); 
       Alcim.setText(adatom.getSzakma()); 

      } 
      return convertView; 
     } 
    } 

And it is my MainActivity: 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
    ... 
    betolt(); 
} 

private void betolt() 
    { 
     ArrayList<Adat> adataim = new ArrayList<>(); 
     adataim.add(new Adat("Title","Subtitle")); 
     adataim.add(new Adat("Title2","Subtitle2")); 
     final ListView myList= (ListView) findViewById(R.id.listView); 
     EgyeniAdapter egyeniAdapter = new EgyeniAdapter(this,adataim); 

     myList.setAdapter(egyeniAdapter); 

    } 

Dies funktioniert, aber wenn ich will Verwenden Sie es in einer anderen Anwendung in Tabbed-Aktivität, es wird ausgelöst:

Nullpointer: Der Versuch, virtuelle Methode 'Leere android.widget.ListView.setAdapter (android.widget.ListAdapter)' auf einer null Objektreferenz

aufzurufen Was ist das Problem?

Mein Fragment Code:

<FrameLayout 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="com.example.mpet8.englishtest.Tesztek"> 

    <!-- TODO: Update blank fragment layout --> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" 
     android:id="@+id/tesztek_label" 
     /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tesztek_listView" 
     android:layout_gravity="left|top" /> 

</FrameLayout> 

ich meine Methode in onStart Methode aufrufen:

@Override 
    protected void onStart() { 

     super.onStart(); 
     betolt(); 
    } 
+2

Mögliche Duplikate von [Was ist ein Null Zeiger Ausnahme und wie kann ich es beheben?] (http: // stackoverfl ow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-fix-it) –

Antwort

0

in betolt() Sie suchen es für eine Listview mit der ID R.id.listView (final ListView myList= (ListView) findViewById(R.id.listView);), aber in Ihrem fragmentlayout hat die ID android:id="@+id/tesztek_listView"

+0

Sry, ich kopierte die normale Aktivität betolt() -Methode. Ich benutzte dies in Registerkarten Aktivität: final ListView myList = (ListView) findViewById (R.id.tesztek_listView); – Petya

+0

Meiner Meinung nach: Die EgyeniAdapter-Klasse ist in Ordnung. Das Problem ist, dass myList null ist. (Ich benutze fragmant in der Tabbed-Ansicht.) private void betolt() { .. ListView myList = (ListView) findViewById (R.id.tesztek_listView); – Petya

Verwandte Themen