我不知道为什么我得到一个NullPointerException,但它发生在spinner.setAdapter(arrayAdapter)行.任何人都可以看看我的代码,看看问题是什么以及如何解决它.提前致谢. 这是我的代码: public class Sli
这是我的代码:
public class SlidingTabsActivity extends SherlockFragmentActivity { ViewPager viewPager; TabsAdapter tabsAdapter; ActionBar actionBarTabs; Spinner spinner; ArrayAdapter<String> arrayAdapter; PopupFirmware popupFirmware; // Popup firmware class instance @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); viewPager = new ViewPager(this); viewPager.setId(R.id.pager); setContentView(viewPager); arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.device_description); spinner = (Spinner) findViewById(R.id.tabsSpinner); spinner.setAdapter(arrayAdapter); actionBarTabs = getSupportActionBar(); actionBarTabs.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBarTabs.setCustomView(spinner); actionBarTabs.setDisplayShowCustomEnabled(true); actionBarTabs.setDisplayHomeAsUpEnabled(true); tabsAdapter = new TabsAdapter(this, viewPager); // Declares the tabs adapter class with the view pager view popupFirmware = new PopupFirmware(this); // Declaring popup firmware class /* Adds fragments to the tabs adapter */ tabsAdapter.addTab(actionBarTabs.newTab().setText("PV"), Fragment_1.class, null); tabsAdapter.addTab(actionBarTabs.newTab().setText("CONFIG"), Fragment_2.class, null); tabsAdapter.addTab(actionBarTabs.newTab().setText("DIAG"), Fragment_3.class, null); }
这是logcat:
11-20 13:16:26.259: E/AndroidRuntime(11789): FATAL EXCEPTION: main 11-20 13:16:26.259: E/AndroidRuntime(11789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobileapplicationretry/com.example.mobileapplicationretry.SlidingTabsActivity}: java.lang.NullPointerException 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.os.Handler.dispatchMessage(Handler.java:99) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.os.Looper.loop(Looper.java:137) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread.main(ActivityThread.java:5103) 11-20 13:16:26.259: E/AndroidRuntime(11789): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 13:16:26.259: E/AndroidRuntime(11789): at java.lang.reflect.Method.invoke(Method.java:525) 11-20 13:16:26.259: E/AndroidRuntime(11789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 11-20 13:16:26.259: E/AndroidRuntime(11789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 11-20 13:16:26.259: E/AndroidRuntime(11789): at dalvik.system.NativeStart.main(Native Method) 11-20 13:16:26.259: E/AndroidRuntime(11789): Caused by: java.lang.NullPointerException 11-20 13:16:26.259: E/AndroidRuntime(11789): at com.example.mobileapplicationretry.SlidingTabsActivity.onCreate(SlidingTabsActivity.java:51) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.Activity.performCreate(Activity.java:5133) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 11-20 13:16:26.259: E/AndroidRuntime(11789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 11-20 13:16:26.259: E/AndroidRuntime(11789): ... 11 more你的微调器是空的.
看,首先创建ViewPager并创建setContentView:
setContentView(viewPager);
但是你的ViewPager是空的,因为你没有放任何东西.接下来你打电话:
spinner = (Spinner) findViewById(R.id.tabsSpinner);
这意味着findViewById将查找您的微调器的起始根视图 – 您之前创建的ViewPager是空的.所以Spinner永远不会被发现 – 它总是空的.