我正在创建一个Android应用程序,但每次我尝试运行它时,我在第2行得到一个NullPointerException。可能导致这个?我肯定在build.xml文件中有微调器。当你调用这样的对象时它是不是被初始化了?
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>
这是我的日志:
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) –
答案 0 :(得分:0)
如果您说NullPointerException
@第2行即spnLocale.setOnItemSelectedListener(new OnSelectListener());
,则spnLocale
为空。
您可能引用了错误的布局。即,您为活动设置的布局没有带有ID的微调器。
编辑:
View rootView = inflater.inflate(R.layout.build,
container, false);
spnLocale = (Spinner)rootView.findViewById(R.id.spinner1);
return rootView;
答案 1 :(得分:0)
您需要先将Adapter设置为spinner,然后才能使用onItemSelected Listener ...这可能会对您有所帮助
@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
}
});
}
答案 2 :(得分:0)
问题是我使用了滑动Tabs,所以我不得不使用rootView.findViewbyId()而不仅仅是findviewbyID