空对象引用上的ListView.setAdapter(android.widget.ListAdapter)'

时间:2019-10-17 11:18:36

标签: java android-studio listview adapter

我想要一个列表视图。但是我遇到了错误。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.photoapp/com.example.photoapp.ViewAdmins}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

这是我的代码。

 public class ViewAdmins extends AppCompatActivity {

    SyncAdminsDatabaseHelper mSyncAdminsDatabaseHelper;
    private ListView ymListView;

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

        ymListView = (ListView) findViewById(R.id.listviewadmins);
        mSyncAdminsDatabaseHelper = new SyncAdminsDatabaseHelper(this);

        //call method populateListView();
        populateListView();

    }
    private void populateListView(){

        //get the data to append in the list view
        Cursor data = mSyncAdminsDatabaseHelper.getData();

        if(data.getCount() == 0){
            toastMessage("The database is empty.");
        }else{

            //Create an Array list
            ArrayList<String> listData = new ArrayList<>();

            //Store the result in array using while loop
            while (data.moveToNext()){
                //get the value from the database in column 1
                //than add to array list
                listData.add(data.getString(0));

            }
            System.out.println(listData);
//        //create the list adapter and set the adapter
            ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,listData);
           ymListView.setAdapter(adapter);


        }


    }

我想将数据从应用程序表显示到列表视图。但是由于某种原因,我的列表视图无法正确呈现数据。

我的问题是如何从SQLite表获取数据并在列表视图中显示。

以及如何在列表视图中显示多个字段。

Activity_view_admins.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ViewAdmins">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listviewadmins">

    </ListView>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

这是我几年前所做的,但不是从SQL lite读取,而是从Web服务返回了即时消息。但它应该使您了解如何填充列表视图。

   public class MainActivity extends AppCompatActivity {
    private static final String url = "http://10.0.2.2/android_app/urltomywebservice";
    private ProgressDialog dialog;
    private List<Item> array = new ArrayList<Item>();
    private ListView listView;
    private Adapter adapter;

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

        listView = (ListView) findViewById(R.id.list_item);
        adapter=new Adapter(this,array);
        listView.setAdapter(adapter);

        dialog=new ProgressDialog(this);
        dialog.setMessage("Loading...");
        dialog.show();

        //Creat volley request obj
        JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                hideDialog();
                //parsing json
                for(int i=0;i<response.length();i++){
                    try{
                        JSONObject obj=response.getJSONObject(i);
                        Item item=new Item();
                        item.setTitle(obj.getString("Title"));
                        item.setImage(obj.getString("Image"));
                        item.setRate(obj.getInt("rating"));
                        item.setPrice(obj.getInt("Price"));



                        //add to array
                        array.add(item);
                    }catch(JSONException ex){
                        ex.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        AppController.getmInstance().addToRequesQueue(jsonArrayRequest);
    }
    public void hideDialog(){
        if(dialog !=null){
            dialog.dismiss();
            dialog=null;
        }
    }

    }

这里添加到listview的数组是我用于设置和获取所需数据的模型的列表。您可以创建适合自己的类似内容。

private List<Item> array = new ArrayList<Item>();