修复Android NullPointerException
我正在创建一个android应用程序,但每次我尝试运行它时,第2行就会出现一个NullPointerException。这可能是什么原因造成的?我确信build.xml文件中有微调器。当你调用一个像这样的对象时,它没有被初始化吗修复Android NullPointerException,android,nullpointerexception,android-spinner,Android,Nullpointerexception,Android Spinner,我正在创建一个android应用程序,但每次我尝试运行它时,第2行就会出现一个NullPointerException。这可能是什么原因造成的?我确信build.xml文件中有微调器。当你调用一个像这样的对象时,它没有被初始化吗 public void onStart(){ spnLocale = (Spinner)findViewById(R.id.spinner1); //1 spnLocale.setOnItemSelectedListener(new OnSelect
public void onStart(){
spnLocale = (Spinner)findViewById(R.id.spinner1); //1
spnLocale.setOnItemSelectedListener(new OnSelectListener()); //2
}
public static class MainSectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public MainSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1){
View rootView = inflater.inflate(R.layout.browse_layout,
container, false);
Spinner spnLocale = (Spinner)findViewById(R.id.spinner1);
spnLocale.setOnItemSelectedListener(new OnSelectListener());
return rootView;
}
else {//if(getArguments().getInt(ARG_SECTION_NUMBER) == 3){
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.setText("help screen");
return rootView;
}
}
}
以下是我的xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:entries="@array/state_array"
android:prompt="@string/state_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
如果您说
NullPointerException
@line 2 iespnLocale.setOnItemSelectedListener(新的OnSelectListener())代码>然后spnLocale
为空
您可能引用了错误的布局。即设置为活动的布局没有具有所述id的微调器
编辑:
您需要先将适配器设置为微调器,然后才能使用选定的侦听器。。。这可能对你有帮助
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
String str[]={"Dummy","Dummy","Dummy"};
spnLocale = (Spinner)findViewById(R.id.spinner1); //1
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,str);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnLocale.setAdapter(adapter);
spnLocale.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@覆盖
受保护的void onStart(){
//TODO自动生成的方法存根
super.onStart();
字符串str[]={“Dummy”、“Dummy”、“Dummy”};
spnLocale=(微调器)findViewById(R.id.spinner1);//1
ArrayAdapter=newArrayAdapter(MainActivity.this,android.R.layout.simple\u微调器\u项,str);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spnLocale.setAdapter(适配器);
setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
//TODO自动生成的方法存根
}
@凌驾
未选择公共无效(AdapterView arg0){
//TODO自动生成的方法存根
}
});
}
问题是我使用的是滑动选项卡,因此我必须使用rootView.findViewbyId()而不仅仅是findViewbyId您确定在onCreate
方法中引用了正确的布局吗<代码>设置内容视图(R.layout.build)代码>?03-22 03:40:17.242:E/AndroidRuntime(2101):由以下原因引起:java.lang.NullPointerException 03-22 03:40:17.242:E/AndroidRuntime(2101):位于com.XXXX.list.MainActivity.onStart(MainActivity.java:175)03-22 03:40:17.242:E/AndroidRuntime(2101):位于android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)03-22 03:40:17.242:E/AndroidRuntime(2101):在android.app.Activity.performStart(Activity.java:5241)03-22 03:40:17.242:E/AndroidRuntime(2101):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)@user3016349第175行是什么?@raghundandan第175行是我标记为//2@cosmincalistru我有刷卡标签。“构建”是其中一个选项卡。我尝试过像您所说的那样更改内容视图,但还是出现了同样的错误。我如何更改我引用的布局?@user3016349您设置为活动的任何布局都必须具有id为spinner1
的微调器。发布完整的活动代码我希望这能奏效!但它说,“不能对非静态字段spnLocale进行静态引用”。但如果我将spnlocale更改为静态,它会说它不能调用非静态方法findviewbyId@user3016349为什么它是静态的。您需要发布整个代码,否则将被遗忘help@user3016349您有一个静态的内部类。将其移动到另一个.java类并移除staticSpinner1,它在xml文件中初始化,并在strings.xml文件中列出条目。如何在不删除spinner1中已有的数据的情况下设置适配器?@user3016349 spinner未在xml中初始化。它是一个ui元素。您可以在活动中初始化微调器。好的,我尝试了,但没有得到相同的错误,除了在这一点:spnLocale.setAdapter(adapter)@user3016349如果您有类似于android:entried=@array/elements
的元素,则不需要adapter@Raghunandan我有这样的元素。
View rootView = inflater.inflate(R.layout.build,
container, false);
spnLocale = (Spinner)rootView.findViewById(R.id.spinner1);
return rootView;
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
String str[]={"Dummy","Dummy","Dummy"};
spnLocale = (Spinner)findViewById(R.id.spinner1); //1
ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,str);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnLocale.setAdapter(adapter);
spnLocale.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}