我的微调器没有工作

时间:2013-08-27 04:47:15

标签: android android-spinner

我创建了一个spinner并且它无法正常工作,我已经使用选项卡创建了它,点击了我想要获取下拉列表的选项卡图标,但现在单击应用程序突然获得的图标关闭用空指针异常说错误我确定我错过了一些愚蠢的东西!我在下面给我的logcat和java类 我需要建议来清除错误。

08-27 04:59:49.423: E/AndroidRuntime(1011): FATAL EXCEPTION: main

08-27 04:59:49.423: E/AndroidRuntime(1011): java.lang.RuntimeException: Unable to start        
activity ComponentInfo{com.neochat/com.neochat.ComposeActivity}:  
java.lang.NullPointerException

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.ActivityThread.startActivityNow(ActivityThread.java:2023)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at  
android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.widget.TabHost.setCurrentTab(TabHost.java:413)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.view.View.performClick(View.java:4204)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.view.View$PerformClick.run(View.java:17355)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.os.Handler.handleCallback(Handler.java:725)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.os.Handler.dispatchMessage(Handler.java:92)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.os.Looper.loop(Looper.java:137)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.ActivityThread.main(ActivityThread.java:5041)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
java.lang.reflect.Method.invokeNative(Native Method)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
java.lang.reflect.Method.invoke(Method.java:511)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
dalvik.system.NativeStart.main(Native Method)

08-27 04:59:49.423: E/AndroidRuntime(1011): Caused by: java.lang.NullPointerException

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
com.neochat.ComposeActivity.onCreate(ComposeActivity.java:29)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.Activity.performCreate(Activity.java:5104)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

08-27 04:59:49.423: E/AndroidRuntime(1011):     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

08-27 04:59:49.423: E/AndroidRuntime(1011):     ... 18 more

ComposeActivity.java

              public class ComposeActivity extends Activity {

             /** An array of strings to populate dropdown list */
             String[] actions = new String[] {
            "Bookmark",
             "Subscribe",
             "Share"
             };
              Context context;
             /** Called when the activity is first created. */
            @Override
              public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.extrasettings);
                 Spinner mySpinner= (Spinner) findViewById(R.id.spinner1);
               context=this;

           /** Create an array adapter to populate dropdownlist */
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_spinner_dropdown_item, actions);

            /** Enabling dropdown list navigation for the action bar */
            getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

            /** Defining Navigation listener */
             ActionBar.OnNavigationListener navigationListener = new
              OnNavigationListener() {

           @Override
             public boolean onNavigationItemSelected(int itemPosition, long itemId) {
            Toast.makeText(getBaseContext(), "You selected : " + actions[itemPosition] 
              , Toast.LENGTH_SHORT).show();
            return false;
                }
               };

           /** Setting dropdown items and item navigation listener for the actionbar */
                getActionBar().setListNavigationCallbacks(adapter, navigationListener);
               }
               }

我添加了微调器的XML文件

             <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" 
                   >

             <Spinner
              android:id="@+id/spinner1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content" />

             </LinearLayout>

我在我的应用程序上使用了一个简单的选项卡选项而不是操作栏,所以有任何问题....对不起我想将微调器添加到我的tabhost中,所以任何人都可以告诉我应该做什么更改在代码中?

2 个答案:

答案 0 :(得分:0)

您获得的异常是空指针异常。请调试并检查是否已写入

Spinner mySpinner= (Spinner) findViewById(R.id.spinner1);

并且“actions”的值不为空。

答案 1 :(得分:0)

在onCreate方法中实例化mySpinner。