简体   繁体   English

findViewById在三星平板电脑上返回null

[英]findViewById returns null on Samsung tablet

seems to be a frequent problem with many solutions proposed but I don't succeed in solving it. 提出许多解决方案似乎是一个经常出现的问题,但我未能成功解决。

all calls to findViewById are successful except the one where expandableListview is created. 除了创建expandableListview的调用之外,所有对findViewById的调用均成功。 And it seems to be device dependent. 它似乎与设备有关。 It works in Android Studio, also on a Moto E 4G (5.0.2) but not on a Samsung Galaxy Tab 8.0 (5.1.1). 它可以在Android Studio中运行,也可以在Moto E 4G(5.0.2)上运行,但不能在Samsung Galaxy Tab 8.0(5.1.1)上运行。

The full project can be found on github : https://github.com/JohanDegraeve/helpdiabetes-android The module that has the problem is https://github.com/JohanDegraeve/helpdiabetes-android/blob/master/app/src/main/java/com/hippoandfriends/helpdiabetes/Show/Food/ShowSelectedFood.java If anyone could clone the project and try to fix this, it would be very helpful. 完整的项目可以在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如果有人可以克隆该项目并尝试解决此问题,那将非常有帮助。

Here the onCreate method, creating expandablelistview returns null. 这里的onCreate方法,创建expandablelistview返回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);
                }
            });
}

solved 解决了

actually on a tablet another layout was used where an id was missing 实际上在平板电脑上,在缺少ID的情况下使用了另一种布局

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

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