the problem was I was using swipe Tabs, so I had to use rootView.findViewbyId() instead of just findviewbyID
Fixing Android NullPointerException
-
19-06-2023 - |
質問
i am creating an android app right, but every time I try to run it, I get a NullPointerException at line 2. What could be causing this? I for sure have the spinner in my build.xml file. When you call an object like this is it not getting initialized?
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;
}
}
}
Here is my 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>
Here is my log :
03-22 03:40:17.242: E/AndroidRuntime(2101): Caused by: java.lang.NullPointerException
03-22 03:40:17.242: E/AndroidRuntime(2101): at com.XXXX.list.MainActivity.onStart(MainActivity.java:175)
03-22 03:40:17.242: E/AndroidRuntime(2101): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
03-22 03:40:17.242: E/AndroidRuntime(2101): at android.app.Activity.performStart(Activity.java:5241)
03-22 03:40:17.242: E/AndroidRuntime(2101): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168) –
解決 3
他のヒント
You need to set Adapter to spinner before you can use onItemSelected Listener... this might help you
@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
}
});
}
If you say NullPointerException
@ line 2 ie spnLocale.setOnItemSelectedListener(new OnSelectListener());
then spnLocale
is null.
You might have referenced the wrong layout. ie the layout you have set to the activity does not have the spinner with the id mentioned.
Edit:
View rootView = inflater.inflate(R.layout.build,
container, false);
spnLocale = (Spinner)rootView.findViewById(R.id.spinner1);
return rootView;
所属していません StackOverflow