0

This code works in basic Activity, but it doesn't in Tabbed activity. I want to use the ListView with custom view. Common code:

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>

It is my simple data class, I want to show it:

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;
}

And my custom 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);

    }

This works, but when I want use it in another application in Tabbed activity, it throws:

NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

What is the problem?

My 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>

I call my method in onStart method:

@Override
    protected void onStart() {

        super.onStart();
        betolt();
    }
ebarrenechea
  • 3,775
  • 1
  • 31
  • 37
Petya
  • 1
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – David Medenjak Apr 16 '16 at 13:11

1 Answers1

0

in betolt() you are looking for a ListView with the id R.id.listView (final ListView myList= (ListView) findViewById(R.id.listView);) but in your fragmentlayout it has the id android:id="@+id/tesztek_listView"

Ralph Bergmann
  • 3,015
  • 4
  • 30
  • 61
  • Sry, I copied the normal activity betolt() method. I used this in tabbed activity: final ListView myList= (ListView) findViewById(R.id.tesztek_listView); – Petya Apr 17 '16 at 07:57
  • In my opinion: The EgyeniAdapter class is OK. The problem is that: myList is null. (I use fragmant in Tabbed view.) private void betolt() { .. ListView myList= (ListView) findViewById(R.id.tesztek_listView); – Petya Apr 17 '16 at 09:21