这似乎是一个常见的问题,提出了许多解决方案,但我没有成功地解决它。
除了创建expandableListview的调用之外,所有对findViewById的调用都是成功的。而且它似乎依赖于设备。它适用于Android Studio,也适用于Moto E 4G (5.0.2),但不适用于三星Galaxy Tab 8.0 (5.1.1)。
完整的项目可以在github上找到:https://github.com/JohanDegraeve/helpdiabetes-android有问题的模块是https://github.com/JohanDegraeve/helpdiabetes-android/blob/master/app/src/main/java/com/hippoandfriends/helpdiabetes/Show/Food/ShowSelectedFood.java如果有人可以克隆这个项目并尝试修复它,那将是非常有帮助的。
在这里,创建expandablelistview的onCreate方法返回null。
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View contentView = LayoutInflater.from(getParent()).inflate(
            R.layout.show_selected_food, null);
    setContentView(contentView);
    // track we come here
    ActivityGroupMeal.group.parent
            .trackPageView(TrackingValues.pageShowSelectedFood);
    expandOrNotDBAdapater = new DbAdapter(this);
    expandOrNotDBAdapater.open();
    functions = new Functions();
    mCalendar = Calendar.getInstance();
    btDelete = (Button) findViewById(R.id.buttonDelete);
    btSaveTemplate = (Button) findViewById(R.id.buttonSaveAsTemplate);
    btLoadTemplate = (Button) findViewById(R.id.buttonLoadTemplate);
    btAddSelectedFoodToTracking = (Button) findViewById(R.id.buttonAddSelectedFoodToTracking);
    btBack = (Button) findViewById(R.id.buttonBack);
    btBack.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            ActivityGroupMeal.group.back();
        }
    });
    expandableListview = (ExpandableListView) findViewById(R.id.expandableListview);
    saveFoodAmount = false;
    fInsulineRatio = 0f;
    fCorrectionFactor = 0f;
    fCalculatedInsulineAmount = 0f;
    dbHelper = new DbAdapter(this);
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    adapter = new CustomExpandableListAdapter(this,
            ActivityGroupMeal.group.getFoodData().dbFontSize,
            metrics.densityDpi);
    listOfSelectedFood = new ArrayList<DBSelectedFood>();
    registerForContextMenu(getListView());
    btAddSelectedFoodToTracking.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            onClickAddSelectedFoodToTracking();
        }
    });
    btDelete.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // track we come here
            ActivityGroupMeal.group.parent.trackEvent(
                    TrackingValues.eventCategoryMeal,
                    TrackingValues.eventCategoryMealDeleteSelectedFood);
            onClickDeleteAll(v);
        }
    });
    btSaveTemplate.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // track we come here
            ActivityGroupMeal.group.parent.trackEvent(
                    TrackingValues.eventCategoryMeal,
                    TrackingValues.eventCategoryMealSaveTemplate);
            onClickSaveAsTemplate(v);
        }
    });
    btLoadTemplate.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // track we come here
            ActivityGroupMeal.group.parent.trackEvent(
                    TrackingValues.eventCategoryMeal,
                    TrackingValues.eventCategoryMealLoadTemplate);
            onClickLoadTemplate(v);
        }
    });
    expandableListview
            .setOnGroupExpandListener(new OnGroupExpandListener() {
                public void onGroupExpand(int groupPosition) {
                    setExpand(1);
                }
            });
    expandableListview
            .setOnGroupCollapseListener(new OnGroupCollapseListener() {
                public void onGroupCollapse(int groupPosition) {
                    setExpand(0);
                }
            });
}发布于 2016-05-08 06:12:43
已解决
实际上,在平板电脑上,缺少id的地方使用了另一种布局
https://stackoverflow.com/questions/37038512
复制相似问题