Use this code may it work for you as below:-
mDrawerList.setAdapter(mNavDrawerListAdapter);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
private ArrayList<HashMap<String, String>> siteArray;
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position<=3){
displayView(position, "","");
}else{
String siteName = mArrayListLat.get(position-4).toString();
siteArray=new DatabaseSites(NavigationActivity.this).getdata();
for(int s=0; s<siteArray.size(); s++){
if(siteArray.get(s).get("name").equals(siteName)){
String siteParam=siteArray.get(s).get("api");
displayView(position, siteName,siteParam);
}
}
}
}
});
private void displayView(int position, String siteName, String sApi) {
Fragment fragmentChild = null;
String barTitle = null;
Bundle mBundle = new Bundle();
switch (position) {
case 0:
fragmentChild = new ProfileFragment();
mBundle.putString(Constants.USER_ID, "");
fragmentChild.setArguments(mBundle);
barTitle = Constants.MY_PROFILE;
break;
default:
fragmentChild = new QuestionFragment();
break;
}
if (fragmentChild != null) {## Heading ##
FragmentManager fragmentManager1 = getSupportFragmentManager();
fragmentManager1.beginTransaction().replace(R.id.frame_container, fragmentChild).addToBackStack(null).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
try {
setTitle(barTitle);
} catch (Exception e) {
setTitle(Constants.STACKY_LITE);
e.printStackTrace();
}
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
有一個空指針異常。它在行listView.setAdapter(adapweekday)崩潰;?如果是這樣,你可以檢查「listView」是否不爲空? – henry000
我只是檢查「listView」,發現它是一個佈局問題 ,它是固定的!謝謝!! – user3810824