尝试在空对象引用上调用虚拟方法'void android.widget.AdapterView.setEmptyView(android.view.View)'

时间:2019-03-13 04:24:19

标签: android

我在1个片段中有1个listview。当我在MainActivity中调用该列表视图时,出现错误“ java.lang.NullPointerException:尝试在引用对象null上调用虚拟方法'void android.widget.AdapterView.setEmptyView(android.view.View)'”

第一行错误:

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

请仔细检查我的代码并提供反馈,我该如何解决?

这是我的代码:

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

0 个答案:

没有答案