带ListView的导航抽屉(无法启动活动ComponentInfo)

时间:2014-07-07 02:46:15

标签: android

我正在尝试创建导航抽屉,其默认视图为ListView

但是,当我设置List的适配器时,它只会出现如下的错误......

07-07 10:22:34.318: E/AndroidRuntime(26075): FATAL EXCEPTION: main
07-07 10:22:34.318: E/AndroidRuntime(26075): Process: com.example.android.navigationdrawerexample, PID: 26075
07-07 10:22:34.318: E/AndroidRuntime(26075): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.MainActivity}: java.lang.NullPointerException 

我的代码有问题吗?

MainActivity.java: http://pastebin.com/pVXKLe7t

1 个答案:

答案 0 :(得分:0)

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");
        }
    }