单击值时,微调器不会更改

时间:2018-01-08 02:33:16

标签: java android

我正在开发Android Studio中的应用。我的一个片段上有一个简单的微调器。我加载了像arraylist这样的数据

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.content_main, container, false);

    Spinner ddlLocation = (Spinner) myView.findViewById(R.id.ddlLocation);
    //ddlLocation.setOnItemSelectedListener(this);

    //Load the nameList array into the drop down list (Spinner)
    LocationDBHandler locationDBHandler = new LocationDBHandler(getActivity().getApplicationContext(),null,null,1);
    ArrayList<String> nameList = locationDBHandler.getAllNames();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, nameList);
    ddlLocation.setPrompt("Select a Location");
    ddlLocation.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    return myView;
}

当我运行应用程序时,我可以点击微调器旁边的箭头,我看到了我的arraylist项目,很棒。当我点击一个项目时,它不会改变Spinner中显示的内容(它不是我的文本颜色)。此外,在尝试访问Spinner中的内容时,我得到空指针错误。

导航页面并从下拉列表中选择项目后的Logcat。

01-07 21:42:43.754 20554-20567/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-07 21:42:43.754 20554-20567/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaee534c0, error=EGL_SUCCESS
01-07 21:42:54.747 20554-20567/com.example.connor.connorminstafeed W/EGL_emulation: eglSurfaceAttrib not implemented
01-07 21:42:54.747 20554-20567/com.example.connor.connorminstafeed W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab6abd00, error=EGL_SUCCESS
01-07 21:42:56.464 20554-20567/com.example.connor.connorminstafeed E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab0f6b20

空指针

01-07 21:49:15.463 22000-22000/com.example.connor.connorminstafeed E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.connor.connorminstafeed, PID: 22000
                                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.Spinner.getSelectedItem()' on a null object reference
                                                                                     at com.example.connor.connorminstafeed.HistoryFragment.onCreateView(HistoryFragment.java:38)
                                                                                     at android.app.Fragment.performCreateView(Fragment.java:2220)
                                                                                     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
                                                                                     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
                                                                                     at android.app.BackStackRecord.run(BackStackRecord.java:793)
                                                                                     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
                                                                                     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

阵列列表代码。不会认为这将是问题,因为Spinner正在填充好吗?

public ArrayList<String> getAllNames() {
    String query = "SELECT name FROM " + TABLE_LOCATION;

    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(query, null);

    ArrayList<String> nameList = new ArrayList<String>();

    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            nameList.add(name);
            cursor.moveToNext();
        }
    }
    return nameList;
}

Spinner XML

    <Spinner
    android:id="@+id/ddlLocation"
    android:layout_width="280dp"
    android:layout_height="83dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:textColor="#475afd"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView11" />

有什么想法吗?

非常感谢。

1 个答案:

答案 0 :(得分:1)

让你的名字列表全局。并在下面的onitemselected中编写代码并检查所选项目是否来了。如果是,则继续。

 @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        Toast.makeText(this,""+nameList.get(i),Toast.LENGTH_LONG).show();
    }

并在你的xml中更改spinner.like下面的背景颜色

机器人:背景= “#676767”

<Spinner
    android:background="#676767"
    android:id="@+id/ddlLocation"
    android:layout_width="280dp"
    android:layout_height="83dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:textColor="#475afd"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView11" />