0

我创建了一个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>

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

4

2 回答 2

0

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

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

并且“动作”的值不为空。

于 2013-08-27T05:00:42.180 回答
0

在 onCreate 方法中实例化 mySpinner。

于 2013-08-27T05:23:27.127 回答