使用 SQLite 数据库错误填充可扩展列表视图

Posted

技术标签:

【中文标题】使用 SQLite 数据库错误填充可扩展列表视图【英文标题】:Populate Expandable ListView with SQLite database error 【发布时间】:2019-12-12 16:45:19 【问题描述】:

我是 android 开发的新手。我正在使用 Expandable listvew 来显示我的数据库中的位置,并且我正在扩展 BaseExpandableListAdapter 但它似乎强制关闭并给出了我在下面添加的错误。

适配器类

package com.example.project.Adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.example.project.R;

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

public class Expandable_List_Adapter extends BaseExpandableListAdapter 
    private Context context;
    private List<String> listDataHeader;
    private HashMap<String,List<String>> _listDataChild;

    public Expandable_List_Adapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> _listDataChild) 
        this.context = context;
        this.listDataHeader = listDataHeader;
        this._listDataChild = _listDataChild;
    


    @Override
    public int getGroupCount() 
        return this.listDataHeader.size();
    
    @Override
    public int getChildrenCount(int groupPosition) 
        return this._listDataChild.get(this.listDataHeader.get(groupPosition)).size();
    
    @Override
    public Object getGroup(int groupPosition) 
        return this.listDataHeader.get(groupPosition);
    

    @Override
    public Object getChild(int groupPosition, int childPosition) 
        return this._listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);
    

    @Override
    public long getGroupId(int groupPosition) 
        return groupPosition;
    


    @Override
    public long getChildId(int groupPosition, int childPosition) 
        return childPosition;
    

    @Override
    public boolean hasStableIds() 
        return false;
    

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
        String headerTitle=(String) getGroup(groupPosition);
        if (convertView==null)
            LayoutInflater layoutInflater=(LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=layoutInflater.inflate(R.layout.expand_header,null);
        
        TextView txtListH=convertView.findViewById(R.id.expand_list_items);
        txtListH.setText(headerTitle);
         return  convertView;
    

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
        final String childText =(String) getChild(groupPosition,childPosition);
        if(convertView == null)
            LayoutInflater layoutInflater=(LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=layoutInflater.inflate(R.layout.expand_list_items,null);
        
        TextView txtListCh=convertView.findViewById(R.id.expand_list_items);
        txtListCh.setText(childText);
        return  convertView;
    

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) 
        return true;
    

这是我的显示地点的数据库类

public String display_spinner()
        List<String> placeList=new ArrayList<>();
        db=this.getReadableDatabase();
        Cursor c=db.rawQuery("Select "+COL_PLACE_NAME+" from "+TABLE_PLACE,null);
        if(c.moveToFirst())
            do
                placeList.add(c.getString(0));
            while(c.moveToNext());
        
        c.close();
        db.close();
        return placeList.toString();
    

这是我的 AddPlaceActivity 中可扩展视图的实现

 public class AddPlaceActivity extends Activity 
 @Override
    protected void onCreate(Bundle savedInstanceState) 
expandableListView=findViewById(R.id.exp);
        expandableListAdapter=new Expandable_List_Adapter(this,listData_H,listData_Ch);
        getReady();
        expandableListView.setAdapter(expandableListAdapter);
    
public  void getReady()
        listData_H=new ArrayList<>();
        listData_Ch=new HashMap<String,List<String>>();
        listData_H.add("Subjects");
        List<String> subjects=new ArrayList<>();
        subjects.add(databaseSubject.display_spinner());
    

这是 LOGCAT 错误

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
        at com.example.project.Adapters.Expandable_List_Adapter.getGroupCount(Expandable_List_Adapter.java:29)
        at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
        at android.widget.ListView.setAdapter(ListView.java:614)
        at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:601)
        at com.example.project.AddStudentActivity.onCreate(AddStudentActivity.java:162)
        at android.app.Activity.performCreate(Activity.java:7335)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3282) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1970) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7156) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975) 

【问题讨论】:

【参考方案1】:

在您的AddPlaceActivity.OnCreate 中,您似乎在初始化列表之前创建了适配器:

//You use the list here
expandableListAdapter=new Expandable_List_Adapter(this,listData_H,listData_Ch);
//You initialize the list here, AFTER using it
getReady();

尝试切换这两行的顺序。

【讨论】:

切换后我得到 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on an null object reference在 com.example.project.Adapters.Expandable_List_Adapter.getGroupView(Expandable_List_Adapter.java:69) 此错误可能来自以下几行:TextView txtListH=convertView.findViewById(R.id.expand_list_items); txtListH.setText(headerTitle);。检查您的 XML 中是否有声明了 R.id.expand_list_items 的项目。【参考方案2】:

你的问题在这里:

expandableListAdapter=new Expandable_List_Adapter(this,listData_H,listData_Ch);
getReady();

您正在尝试将 listData_H 这个变量设置为您的适配器,然后再在您的 getReady(); 中初始化它

listData_H=new ArrayList&lt;&gt;();应该在expandableListAdapter=new Expandable_List_Adapter(this,listData_H,listData_Ch);之前先执行

否则您将收到 NullPointerException。希望对您有所帮助。

【讨论】:

以上是关于使用 SQLite 数据库错误填充可扩展列表视图的主要内容,如果未能解决你的问题,请参考以下文章

如何在可扩展列表视图中绑定 Json 数据?

Kotlin开发 如何获取SQLite数据库上自己创建规律的可扩展二级列表数据

UiCollectionView 和可扩展视图

如何根据sqlite数据库中的名称从drawable中获取图像,然后在列表视图中显示

如何将可扩展列表视图的选定子视图数据从片段发送到父活动?

具有可扩展列表视图项的Android列表视图