简体   繁体   English

使用BaseActivity在不同活动上使用相同的导航抽屉

[英]Same Navigation Drawer on different Activities using BaseActivity

Im having error integrating my baseactivity class to anu of my activities: 我在将我的基本活动类集成到我的活动的an时出错:

the error says: 错误说:

Caused by: java.lang.NullPointerException at com.test.edc.BaseActivity.onCreate(BaseActivity.java:64) at com.test.edc.ScheduleActivity.onCreate(ScheduleActivity.java:53) 由:com.test.edc.BaseActivity.onCreate(BaseActivity.java:64)处的com.test.edc.ScheduleActivity.onCreate(ScheduleActivity.java:53)处的java.lang.NullPointerException

heres my code: 这是我的代码:

public class BaseActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

public DrawerLayout drawer;
ImageView navDrawerBtn;

HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onCreate(Bundle savedInstanceState) {



    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(402,465);    
    } else {        
        expListView.setIndicatorBoundsRelative(402,465);    
    } 

    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    prepareListData();



    navDrawerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(!drawer.isDrawerOpen(expListView)) {
                drawer.openDrawer(expListView);
                } else {
                    drawer.closeDrawer(expListView);
                }

            }
        });


    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            switch (childPosition) {
            case 0: 
                Intent a = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(a);
                break;

            case 1: 
                Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
                startActivity(b);
                break;
}
            return false;
            // TODO Auto-generated method stub
  //                Toast.makeText(
  //                        getApplicationContext(),
  //                        listDataHeader.get(groupPosition)
  //                                + " : "
  //                                + listDataChild.get(
   //                                            listDataHeader.get(groupPosition)).get(
   //                                            childPosition), Toast.LENGTH_SHORT)
  //                        .show();
  //                return false;
        }
    });
}

/*
 * Preparing the list data
 */
private void prepareListData() {



    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("VRP Medical Bay");
    //listDataHeader.add("");
    //listDataHeader.add("");

    // Adding child data

    List<String> listUnderVRP = new ArrayList<String>();

    listUnderVRP.add("eDataClinical");
    listUnderVRP.add("Schedule");
    listUnderVRP.add("Dictate");
    listUnderVRP.add("View Messages");
    listUnderVRP.add("Reports for Signature");
    listUnderVRP.add("View Billing");
    listUnderVRP.add("View State");


    listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
    //listDataChild.put(listDataHeader.get(1), nowShowing);
    //listDataChild.put(listDataHeader.get(2), comingSoon);
}


 }

heres my code for schedule activity 这是我的时间表活动代码

public class ScheduleActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule);


    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

The problem is onCreate of the base is called before you called setContentView . 问题是在调用setContentView之前先调用onCreate的基础。 And before calling setContentView if you call findViewById it will always return null as no view is added yet. 在调用setContentView之前,如果您调用findViewById ,则由于未添加任何视图,它将始终返回null。 Change your code a bit try this: 稍微更改代码,请尝试以下操作:

create an init function in base activity: 在基本活动中创建一个初始化函数:

public void init(){

 if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(402,465);    
    } else {        
        expListView.setIndicatorBoundsRelative(402,465);    
    } 

    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);

    prepareListData();



    navDrawerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(!drawer.isDrawerOpen(expListView)) {
                drawer.openDrawer(expListView);
                } else {
                    drawer.closeDrawer(expListView);
                }

            }
        });


    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            switch (childPosition) {
            case 0: 
                Intent a = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(a);
                break;

            case 1: 
                Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
                startActivity(b);
                break;
}
            return false;
            // TODO Auto-generated method stub
  //                Toast.makeText(
  //                        getApplicationContext(),
  //                        listDataHeader.get(groupPosition)
  //                                + " : "
  //                                + listDataChild.get(
   //                                            listDataHeader.get(groupPosition)).get(
   //                                            childPosition), Toast.LENGTH_SHORT)
  //                        .show();
  //                return false;
        }
    });
}

And in sub activities override it: 在子活动中覆盖它:

public void init(){
super.init();
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
}

and in onCreate of sub activity: 并在子活动的onCreate中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.schedule);
    init();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM