我尝试使用填充了我的httprequest的数据库中的数据来填充expandablelistview。到目前为止,我已经能够毫无问题地填充数据库。当我尝试设置expandablelistview的适配器时,我收到NullPointerException错误。我需要考虑的另一件事是在我设置适配器之前我必须执行一个线程来检查我的在线数据库,看看它是否是我的手机数据库的某个版本/ uptodate。
这是我的代码:
MainActivity.java
public class MainActivity extends Activity
{
private List<lvItem> myLVItems = new ArrayList<lvItem>();
private List<Parent> myParent = new ArrayList<Parent>();
ExpandableListView exv;
final DBHelper db2 = new DBHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState)
{
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ButtonClick(v);
}
});
db2.open();
dbVersion dbversion = new dbVersion();
dbversion.checkDBVersion(db2, myParent, myLVItems);
}
public void ButtonClick(View v)
{
}
}
dbVersion.java
public class dbVersion
{
DBHelper db2;
List<Parent> parent;
List<lvItem> myLVItems;
ExpandableListView exv;
Context context;
public void checkDBVersion(DBHelper db, List<Parent> myParent, List<lvItem> mylvitems, ExpandableListView exv2, Context context2)
{
this.db2 = db;
this.parent = myParent;
this.myLVItems = mylvitems;
this.context = context2;
this.exv = exv2;
VerCheckThread vercheckthread = new VerCheckThread();
vercheckthread.execute();
}
public class VerCheckThread extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
//This populates my database
}
@Override
protected void onPostExecute(String result)
{
checkdbs(result); //result is current db version
}
}
public void checkdbs(String line)
{
Cursor c = db2.getAllVersionRecords();
if(c.getCount()<1)
{
Log.i("dbVersion, checkdbs", "There is not an entry for the table version");
VerCreateThread vercreatetheard = new VerCreateThread();
vercreatetheard.execute();
}
else
{
c.moveToLast();
String lastid = c.getString(c.getColumnIndex("id"));
lastid.trim();
line.trim();
if(lastid.equals(line))
{
Log.i("dbVersion, checkdbs", "DBs are up-to-date");
lvl2 l2 = new lvl2();
lvl3 l3 = new lvl3();
l3.popLvl3(db2,myLVItems); //this populates myLVItems
l2.popLvl2List(db2,parent); //this populates parent
Log.i("dbVersion, checkdbs", "myLVItems size = "+myLVItems.size()); //checking the size of myLVItems to see if they were populated
Log.i("dbVersion, checkdbs", "parent size = "+parent.size()); //checking the size of parent to see if they were populated
exv.setAdapter(new MyAdapter(context)); //this is causing the error
}
else
{
Log.i("dbVersion, checkdbs", "DBs NOT utd phonedb = "+c.getString(c.getColumnIndex("id"))+ " webdb = "+ line);
db2.updateDB();
VerCreateThread vercreatetheard = new VerCreateThread();
vercreatetheard.execute();
}
}
}
}
MyAdapter.java
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
List<Parent> parent;
private List myLVItems;
private LayoutInflater inflater;
String []groupList = {"Subject One","Subject Two","Subject Three"}; // for now this is to populate the group
public MyAdapter(Context context){
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public Object getChild(int arg0, int arg1)
{
return null;
}
@Override
public long getChildId(int arg0, int arg1)
{
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,ViewGroup parentView)
{
final Parent parent;
lvItem currentLVItem = (lvItem) myLVItems.get(childPosition);
convertView = inflater.inflate(R.layout.db_items, parentView, false);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
imageView.setImageResource(currentLVItem.getIconId());
TextView hiddenView = (TextView) convertView.findViewById(R.id.subIdtextView);
hiddenView.setText(currentLVItem.getId());
TextView summaryView = (TextView) convertView.findViewById(R.id.subSubjectTextView);
summaryView.setText(currentLVItem.getSummary());
TextView descripView = (TextView) convertView.findViewById(R.id.subScriptTextView2);
descripView.setText(currentLVItem.getScripts());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
return childList[groupPosition].length;
}
@Override
public Object getGroup(int arg0)
{
return null;
}
@Override
public int getGroupCount()
{
return groupList.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
TextView tv = new TextView(context);
tv.setText(groupList[groupPosition]);
return tv;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1)
{
return true;
}
}
logcat的:
01-07 21:20:48.141: I/dbVersion, checkdbs(1715): myLVItems size = 18
01-07 21:20:48.141: I/dbVersion, checkdbs(1715): parent size = 4
01-07 21:20:48.141: D/AndroidRuntime(1715): Shutting down VM
01-07 21:20:48.152: W/dalvikvm(1715): threadid=1: thread exiting with uncaught exception (group=0xb5cba908)
01-07 21:20:48.152: E/AndroidRuntime(1715): FATAL EXCEPTION: main
01-07 21:20:48.152: E/AndroidRuntime(1715): java.lang.NullPointerException
01-07 21:20:48.152: E/AndroidRuntime(1715): at com.example.project.dbVersion.checkdbs(dbVersion.java:132)
答案 0 :(得分:0)
您的ExpandableListView为空。您希望将super.onCreate(savedInstanceState);
移到onCreate方法的顶部。在创建活动之前调用findViewById
可能会导致麻烦。