2014-01-07 84 views
0

我試圖用填充我的httprequest的數據庫中的數據填充expandablelistview。到目前爲止,我已經能夠填充數據庫沒有任何問題。當我嘗試設置expandablelistview的適配器時,出現NullPointerException錯誤。我必須考慮的另一件事是,在我可以設置適配器之前,我必須執行一個線程來檢查我的在線數據庫,以查看它是否與我的電話數據庫有關的某些版本/最新版本。ExpandableListView空指針異常

這裏是我的代碼:

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) 
+1

1.是否爲exv null? 2. VerCheckThread的onPostExecute()中有'setAdapter'註釋掉了。這似乎是正確的策略。在點擊按鈕後設置適配器似乎不是一個好主意,因爲您不知道後臺線程是否完成。 –

+0

@MikeOrtiz感謝您的回覆。 1.是的,我想,因爲在MyAdapter被調用之前,exv不會被誇大。 2.那是我第一次想到,然後我開始嘗試讓它運作起來。我應該將「exv」和「this」傳遞給dbVersion ... dbversion.checkDBVersion(db2,myParent,myLVItems,exv,this);我試過這樣做,但仍然出現錯誤。 – user908759

+0

1.在設置適配器的時候,調用'findViewById'後,它不應該爲空。這顯然是NPE的原因,因爲它意味着在xml中找不到ListView。 2.什麼是錯誤? –

回答

0

您的ExpandableListView爲null。您想將super.onCreate(savedInstanceState);移動到onCreate方法的頂部。在創建活動之前,可能會導致您致電findViewById