晚上好,
所以我一无所知。我要了
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'
基本上,我知道空对象引用意味着未分配某些内容(是的,我是编程新手!)。
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.palWerte, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter); //here the error happens
spinner.setOnItemSelectedListener(this);
[...]
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_kaz:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new KalorienzählerFragment()).commit();
break;
case R.id.nav_kar:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new KalorienrechnerFragment()).commit();
break;
case R.id.nav_erp:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ErnährungsplanFragment()).commit();
break;
case R.id.nav_trp:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new TrainingsplanFragment()).commit();
break;
case R.id.nav_trh:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new TrainingshilfeFragment()).commit();
break;
case R.id.nav_ala:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new AlarmFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 0 :(得分:0)
似乎findViewById(R.id.spinner1);
返回了null
。
请尝试在活动中的setContentView
函数中检查布局,您的Spinner
必须在布局中定义ID为“ @ + id / spinner1”
答案 1 :(得分:0)
您的代码:“微调框微调= findViewById(R.id.spinner1);”
这应该是“ Spinner spinner =(Spinner)findViewById(R.id.spinner1);”
或者它是一个片段
“ builder.setView(fragmentView);”
“(Spinner)fragmentView.findViewById(R.id.spinner1);”
希望获得帮助
答案 2 :(得分:0)
可能是您使用了错误的文件。检查您在哪里有微调器,并在哪里尝试设置适配器。
例如,在settings_activity.xml
中存在Spinner,但是您尝试在main_activity.java