我试图来填充expandablelistview,数据从填充我的一个HTT prequest的数据库。到目前为止,我已经能够填充数据库没有任何问题。我得到的NullPointerException错误,当我尝试设置expandablelistview的适配器。我不得不考虑的另一件事是我之前可以设置适配器我必须执行一个线程,会检查我的在线数据库,看它是否是某些版本/ UPTODATE我的电话数据库。
下面是我的code:
MainActivity.java
公共类MainActivity扩展活动{ 私人列表<&LVITEM GT; myLVItems =新的ArrayList<&LVITEM GT;(); 私人列表<家长和GT; myParent =新的ArrayList<家长和GT;(); ExpandableListView EXV; 最后DBHelper DB2 =新DBHelper(本); @覆盖 保护无效的onCreate(捆绑savedInstanceState) { EXV =(ExpandableListView)findViewById(R.id.expandableListView1); super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_main); 按钮BTN =(按钮)findViewById(R.id.button1); btn.setOnClickListener(新View.OnClickListener() { @覆盖 公共无效的onClick(视图v) { ButtonClick(五); } }); db2.open(); dbVersion dbversion =新dbVersion(); dbversion.checkDBVersion(DB2,myParent,myLVItems); } 公共无效ButtonClick(视图V) { }}dbVersion.java
公共类dbVersion{ DBHelper DB2; 清单<家长和GT;父母; 清单<&LVITEM GT; myLVItems; ExpandableListView EXV; 上下文语境; 公共无效checkDBVersion(DBHelper分贝,列表与LT;家长和GT; myParent,列表与LT; LVITEM> mylvitems,ExpandableListView EXV2,上下文context2) { this.db2 = DB; this.parent = myParent; this.myLVItems = mylvitems; this.context = context2; this.exv = EXV2; VerCheckThread vercheckthread =新VerCheckThread(); vercheckthread.execute(); } 公共类VerCheckThread扩展的AsyncTask<字符串,整数,字符串>{ @覆盖 保护字符串doInBackground(字符串... PARAMS) { //这个填充我的数据库 } @覆盖 保护无效onPostExecute(字符串结果) { checkdbs(结果); //结果是当前分贝版本 } } 公共无效checkdbs(串线) { 光标C = db2.getAllVersionRecords(); 如果(c.getCount()&。1) { Log.i(dbVersion,checkdbs,没有该表的版本中的条目); VerCreateThread vercreatetheard =新VerCreateThread(); vercreatetheard.execute(); } 其他 { c.moveToLast(); 字符串lastid = c.getString(c.getColumnIndex(ID)); lastid.trim(); line.trim(); 如果(lastid.equals(线)) { Log.i(dbVersion,checkdbs,区议会均达到最新的); LVL2 L2 =新LVL2(); lvl3 L3 =新lvl3(); l3.popLvl3(DB2,myLVItems); //这个填充myLVItems l2.popLvl2List(DB2,父母); //这个填充母 Log.i(dbVersion,checkdbs,myLVItems大小=+ myLVItems.size()); //检查myLVItems的大小,看是否它们是填充 Log.i(dbVersion,checkdbs,父大小=+ parent.size()); //检查父的大小,看是否它们是填充 exv.setAdapter(新MyAdapter(上下文)); //这是导致错误 } 其他 { Log.i(dbVersion,checkdbs,NOT的DB UTD phonedb =+ c.getString(c.getColumnIndex(ID))+webdb =+线); db2.updateDB(); VerCreateThread vercreatetheard =新VerCreateThread(); vercreatetheard.execute(); } } }}MyAdapter.java
公共类MyAdapter扩展BaseExpandableListAdapter { 私人上下文的背景下; 清单<家长和GT;父母; 私有列表myLVItems; 私人LayoutInflater吹气; 的String [] = GROUPLIST {一个主题,两课,主题三}; //现在这是填充组 公共MyAdapter(上下文的背景下){ this.context =背景; 充气= LayoutInflater.from(上下文); } @覆盖 公共对象getChild(INT为arg0,ARG1 INT) { 返回null; } @覆盖 众长getChildId(INT为arg0,ARG1 INT) { 返回0; } @覆盖 公共查看getChildView(INT groupPosition,诠释childPosition,布尔isLastChild,查看convertView,ViewGroup中parentView) { 最后家长父母; 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()); 返回convertView; } @覆盖 公众诠释getChildrenCount(INT groupPosition) { 返回的childList [groupPosition]。长度; } @覆盖 公共对象getGroup(INT为arg0) { 返回null; } @覆盖 公众诠释getGroupCount() { 返回groupList.length; } @覆盖 众长getGroupId(INT groupPosition) { 返回groupPosition; } @覆盖 公共查看getGroupView(INT groupPosition,布尔isExpanded,查看convertView,父母的ViewGroup) { TextView的电视=新的TextView(背景); tv.setText(GROUPLIST [groupPosition]); 返回电视; } @覆盖 公共布尔hasStableIds() { 返回false; } @覆盖 公共布尔isChildSelectable(INT为arg0,ARG1 INT) { 返回true; }}LogCat中:
21 01-07:20:48.141:I / dbVersion,checkdbs(1715):myLVItems大小= 1801-07 21:20:48.141:I / dbVersion,checkdbs(1715):母公司大小= 401-07 21:20:48.141:D / AndroidRuntime(1715):关闭VM01-07 21:20:48.152:W / dalvikvm(1715):主题ID = 1:螺纹未捕获的异常退出(组= 0xb5cba908)01-07 21:20:48.152:E / AndroidRuntime(1715):致命异常:主要01-07 21:20:48.152:E / AndroidRuntime(1715):显示java.lang.NullPointerException01-07 21:20:48.152:E / AndroidRuntime(1715):在com.example.project.dbVersion.checkdbs(dbVersion.java:132)解决方案
您ExpandableListView为空。要移动 super.onCreate(savedInstanceState); 来onCreate方法的顶部。它可能 findViewById 创建活动之前,会导致你打电话的烦恼。
I trying to populate an expandablelistview, with data from a database that is populated my a httprequest. So far I have been able to populate the database without any problems. I get NullPointerException error when I try to set the adapter of the expandablelistview. Another thing I have to think about is before I can set the adapter I have to execute a thread that will check my online database to see if it is the some version/uptodate with my phone database.
Here is my code:
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)解决方案
Your ExpandableListView is null. You want to move super.onCreate(savedInstanceState); to the top of the onCreate method. It might be causing troubles that you call findViewById before creating the Activity.
更多推荐
ExpandableListView空指针异常
发布评论