我在静态函数的findViewById中发现错误。

我的职能是

public static void onYearSelect() {

    Spinner yearSelector;
    String yearName;
    yearSelector = (Spinner) findViewById(R.id.year_submit);
    yearName = yearSelector.getSelectedItem().toString();

}


有什么办法可以解决这个错误!

我可以将YearName中的Spinner值存储为String吗

最佳答案

您需要在函数内部添加视图作为参数。


public static void onYearSelect(View view) {

    Spinner yearSelector;
    yearSelector = (Spinner) view.findViewById(R.id.year_submit);
    yearName = yearSelector.getSelectedItem().toString();

}


The view which contains your layout data


  调用该函数时,在此处onYearSelect(rootView)添加rootView具有布局视图。


编辑1:


  这里是什么视图?


A View in Android is a widget that displays something. Buttons, listviews, imageviews, etc. are all subclasses of View. When you say "change view" I assume you mean change the layout by using setContentView(). This usually only needs to be done once per activity. An Activity is basically what you are referring to as a screen.

您可以阅读更多here

07-24 14:41