findViewById在Samsung平板电脑上返回null

时间:2016-05-04 21:39:05

标签: android null expandablelistview findviewbyid

似乎是许多解决方案的常见问题,但我没有成功解决它。

对findViewById的所有调用都是成功的,除了创建expandableListview的调用。它似乎取决于设备。它适用于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);
                }
            });
}

1 个答案:

答案 0 :(得分:0)

解决

实际上在平板电脑上,在缺少ID的地方使用了另一种布局

相关问题