列表视图问题:尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

时间:2019-07-04 13:57:03

标签: java android nullpointerexception

我尝试使用简单的适配器将Json Object加载到Listview,但是返回以下错误。

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'

我已经检查了JSON对象是否有效。

这是json对象

{
    "result": [
        {
            "nomor": "18",
            "id_ortu": "020202",
            "alasan_perizinan": "ada aja deh pokoknya",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "17",
            "id_ortu": "020202",
            "alasan_perizinan": "ada aja",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "16",
            "id_ortu": "020202",
            "alasan_perizinan": "ada dehhhhhhh",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        },
        {
            "nomor": "14",
            "id_ortu": "020202",
            "alasan_perizinan": "hs",
            "tgl_izin": "2019-07-10",
            "lama_izin": "10",
            "tgl_kembali": "2019-07-20"
        }
    ]
}

这是我的java

package com.budiluhur.almusyarrofahdigital;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.JsonReader;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DaftarPermintaanIzin extends AppCompatActivity {

    private String JSON_STRING;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = findViewById(R.id.ListPermintaan);

        getJSON();
    }

    private void showIzin() {
        JSONObject jsonObject = null;
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray("result");


            for (int i = 0; i < result.length(); i++) {
                JSONObject jsonObject1 = result.getJSONObject(i);
                String nomor = jsonObject1.getString("nomor");
                String id_ortu = jsonObject1.getString("id_ortu");
                String alasanPerizinan = jsonObject1.getString("alasan_perizinan");
                String tglIzin = jsonObject1.getString("tgl_izin");
                String lamaIzin = jsonObject1.getString("lama_izin");
                String tglKembali = jsonObject1.getString("tgl_kembali");
                HashMap<String, String> perizinan = new HashMap<>();
                perizinan.put("nomor", nomor);
                perizinan.put("id_ortu", id_ortu);
                perizinan.put("alasan_perizinan", alasanPerizinan);
                perizinan.put("tglIzin", tglIzin);
                perizinan.put("lamaIzin", lamaIzin);
                perizinan.put("tgl_kembali", tglKembali);
                list.add(perizinan);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(this, list, R.layout.list_izin, new String[]{"nomor", "id_ortu","alasan_perizinan","tglIzin","lamaIzin","tgl_kembali"}, new int[]{R.id.nomor1, R.id.id_ortu1,R.id.AlasanIzin1,R.id.TglIzin1,R.id.LamaIzin1,R.id.TglKembali1});

        listView.setAdapter(adapter);

    }


    private void getJSON() {
        class getJSON extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();

            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                Toast.makeText(DaftarPermintaanIzin.this,result, Toast.LENGTH_LONG).show();
                JSON_STRING = result;
                showIzin();
            }


            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequest("https://ohmybags.id/almus/getIzin.php");
                return s;
            }
        }
        getJSON getJSON = new getJSON();
        getJSON.execute();
    }



}


这是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:id="@+id/nomor1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"/>

    <TextView
        android:id="@+id/id_ortu1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />



    <TextView
        android:id="@+id/AlasanIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:id="@+id/TglIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />


    <TextView
        android:id="@+id/LamaIzin1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

    <TextView
        android:id="@+id/TglKembali1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="testing"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

</LinearLayout>


我哪里出错了?

1 个答案:

答案 0 :(得分:0)

您必须在setContentView(int)调用之后在onCreate()中调用super,并提供布局资源作为参数。在文档here中了解更多信息。