0

I have 1 listview in 1 fragment. When I call that listview in MainActivity, I get an error "java.lang.NullPointerException: Attempting to invoke virtual method 'void android.widget.AdapterView.setEmptyView (android.view.View)' on a reference object null"

Error at line:

listViewGhiChu. setEmptyView (findViewById (R.id.textViewRong)); and listViewGhiChu.setAdapter (ghiChuAdapter);

Kindly review my code and give feedback, How can I fix this?

This is my code:

**MainActivity class:**

    public class MainActivity extends AppCompatActivity {

    ListView listViewGhiChu;
    ImageView imageViewThem;
    Button buttonXoaTatCa;

    public static Database database;
    public static ArrayList<GhiChu> ghiChuArrayList;
    public static GhiChuAdapter ghiChuAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listViewGhiChu = (ListView) findViewById(R.id.listViewGhiChu);
        imageViewThem = (ImageView) findViewById(R.id.imageViewThem);
        buttonXoaTatCa = (Button) findViewById(R.id.buttonXoaTatCa);
        listViewGhiChu.setEmptyView(findViewById(R.id.textViewRong));

        // Cài đặt ListView và Adapter
        ghiChuArrayList = new ArrayList<>();
        ghiChuAdapter = new GhiChuAdapter(this, R.layout.line_layout_ghichu, ghiChuArrayList);
        listViewGhiChu.setAdapter(ghiChuAdapter);

        // Tạo database
        database = new Database(this, "GhiChu.sqlite", null, 1);
        // Tạo table
        database.QueryData("CREATE TABLE IF NOT EXISTS GhiChutbl(Id INTEGER PRIMARY KEY, TieuDe VARCHAR(200)" +
                ", NoiDung VARCHAR(200))");

     LoadDuLieu();
     SettupViewPager();
    }

    private void SettupViewPager() {
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        ViewPager viewPager = findViewById(R.id.viewPager);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    }

    public static void LoadDuLieu() {
        // Tạo con trỏ duyệt từng phần tử trong table
        Cursor cursor = database.GetData("SELECT * FROM GhiChutbl");
        ghiChuArrayList.clear();

        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String tieude = cursor.getString(1);
            String noidung = cursor.getString(2);
            ghiChuArrayList.add(new GhiChu(id, tieude, noidung));
        }

        ghiChuAdapter.notifyDataSetChanged();
    }

**PagerAdapter class**

    public class PagerAdapter extends FragmentPagerAdapter {

    private int numOfTabs;

    public PagerAdapter(FragmentManager fm, int numOfTabs) {
        super(fm);
        this.numOfTabs = numOfTabs;
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                return new GhiChuFragment();
            case 1:
                return new HinhAnhFragment();
            case 2:
                return new PaintFragment();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return numOfTabs;
    }
}

**activity_main.xml**

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".GhiChu.MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="359dp"
        android:layout_height="46dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TabItem
            android:id="@+id/tabGhiChu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ghi chú" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabHinhAnh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hình ảnh" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabPaint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Vẽ" />
    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="355dp"
        android:layout_height="509dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"
        app:layout_constraintVertical_bias="0.0" />


</android.support.constraint.ConstraintLayout>

**fragment_tab_ghi_chu.xml**

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".TabFragment.TabGhiChu">

    <TextView
        android:id="@+id/textViewRong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginTop="236dp"
        android:text="Chưa có nội dung"
        android:textColor="#000"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listViewGhiChu"
        android:layout_width="359dp"
        android:layout_height="477dp"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageViewThem"
        android:layout_width="65dp"
        android:layout_height="55dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/button_add" />

    <Button
        android:id="@+id/buttonXoaTatCa"
        android:layout_width="96dp"
        android:layout_height="48dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/custom_button"
        android:text="Xóa tất cả"
        android:textAllCaps="false"
        android:textColor="#fff"
        android:textSize="17sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
zmag
  • 7,825
  • 12
  • 32
  • 42
khanhsb
  • 41
  • 6
  • `listViewGhiChu` is in your fragment layout so ofcourse `findViewById(R.id.listViewGhiChu)` in your Activity would return null – Bach Vu Mar 13 '19 at 04:33
  • listViewGhiChu is not there in activity_main. You are trying to call this in MainActivity – Athira Mar 13 '19 at 04:34

3 Answers3

2
  setContentView(R.layout.activity_main);

    listViewGhiChu = (ListView) findViewById(R.id.listViewGhiChu);

listViewGhiChu this ListView is in fragment_tab_ghi_chu.xml. But you are trying to call it in activity_main from MainActivity. Call listview in fragment.

Athira
  • 1,177
  • 3
  • 13
  • 35
2

Sorry bro. You cannot access listViewGhiChu from the activity as it is only declared in the fragment. Your activity does not contain any such view; only your fragment has that view. You should consider moving your adapter implementation to onViewCreated or onCreateView in the fragment.

EDIT:

1

Remove this code from your main activity:

listViewGhiChu = (ListView) findViewById(R.id.listViewGhiChu);
imageViewThem = (ImageView) findViewById(R.id.imageViewThem);
buttonXoaTatCa = (Button) findViewById(R.id.buttonXoaTatCa);
listViewGhiChu.setEmptyView(findViewById(R.id.textViewRong));

// Cài đặt ListView và Adapter
ghiChuArrayList = new ArrayList<>();
ghiChuAdapter = new GhiChuAdapter(this, R.layout.line_layout_ghichu, ghiChuArrayList);
listViewGhiChu.setAdapter(ghiChuAdapter);

// Tạo database
database = new Database(this, "GhiChu.sqlite", null, 1);
// Tạo table
database.QueryData("CREATE TABLE IF NOT EXISTS GhiChutbl(Id INTEGER PRIMARY KEY, TieuDe VARCHAR(200)" +
        ", NoiDung VARCHAR(200))");

LoadDuLieu();
SettupViewPager();

2

Add this code to the onViewCreated method of your fragment:

listViewGhiChu = (ListView) getView().findViewById(R.id.listViewGhiChu);
imageViewThem = (ImageView) getView().findViewById(R.id.imageViewThem);
buttonXoaTatCa = (Button) getView().findViewById(R.id.buttonXoaTatCa);
listViewGhiChu.setEmptyView(getView().findViewById(R.id.textViewRong));

// Cài đặt ListView và Adapter
ghiChuArrayList = new ArrayList<>();
ghiChuAdapter = new GhiChuAdapter(this, R.layout.line_layout_ghichu, ghiChuArrayList);
listViewGhiChu.setAdapter(ghiChuAdapter);

// Tạo database
database = new Database(this, "GhiChu.sqlite", null, 1);
// Tạo table
database.QueryData("CREATE TABLE IF NOT EXISTS GhiChutbl(Id INTEGER PRIMARY KEY, TieuDe VARCHAR(200)" +
        ", NoiDung VARCHAR(200))");

LoadDuLieu();
SettupViewPager();

3

Remove this from your main activity and add it to your fragment the same way you did in your main activity:

ListView listViewGhiChu;
ImageView imageViewThem;
Button buttonXoaTatCa;

public static Database database;
public static ArrayList<GhiChu> ghiChuArrayList;
public static GhiChuAdapter ghiChuAdapter;

Didn't get the chance to test any of this code, but it should work (Unless you are have another error not included in this question).

  • I have declared listview and data in GhiChuFragment but still failed "Attempt to invoke virtual method 'void android.widget.AdapterView.setEmptyView (android.view.View)' on a null object reference" – khanhsb Mar 13 '19 at 05:12
  • Post your fragment code. And completely remove that code from your main activity. It will keep failing. –  Mar 13 '19 at 05:25
  • Thanks for your help, I has solved issue, and fragment has receive data and view. – khanhsb Mar 13 '19 at 05:52
  • You should mark it as the answer if it solved your problem. –  Mar 13 '19 at 06:00
1

You are trying to access your ListView in your activity layout. As per your layout files, list view instance is held by Fragment.

Yashika
  • 173
  • 1
  • 6