获取java.lang.NullPointerException简单地从字符串数组或列表或xml文件(strings.xml)填充微调器

时间:2016-01-27 07:34:48

标签: android nullpointerexception spinner android-arrayadapter android-spinner

我想通过String数组或XML文件(strings.xml)填充 spinner 。但我收到java.lang.NullPointerException:错误。我在谷歌搜索了很多,但它毫无价值。我试过的就像下面一样。

SecondActivity.java

package com.example.android02.personalityinsight;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    TabLayout tabs;
    ViewPager viewPager;
    MyPagerAdapter pagerAdapter;

    Spinner sp1, sp2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

//        String colors[] = {"Red", "Blue", "White", "Yellow", "Black", "Green", "Purple", "Orange", "Grey"};
//        sp1 = (Spinner) findViewById(R.id.sp1);
//
//        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, colors);
//        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        sp1.setAdapter(spinnerArrayAdapter);

        sp2 = (Spinner) findViewById(R.id.sp2);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.ans, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp2.setAdapter(adapter);

        tabs = (TabLayout) findViewById(R.id.tabs);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        submit = (Button) findViewById(R.id.btn_submit);

        tabs.addTab(tabs.newTab().setText("Personality Insights"));
        tabs.addTab(tabs.newTab().setText("Quiz"));
        tabs.addTab(tabs.newTab().setText("PI v/s Quiz"));

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), tabs.getTabCount());
        viewPager.setAdapter(pagerAdapter);
        tabs.setTabsFromPagerAdapter(pagerAdapter);
        tabs.setupWithViewPager(viewPager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                break;
            default:
        }
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

        int mNumOfTabs;

        public MyPagerAdapter(FragmentManager fm, int mNumOfTabs) {
            super(fm);
            this.mNumOfTabs = mNumOfTabs;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return TabOne_Fragment.newInstance();
                case 1:
                    return TanTwo_Fragment.newInstance();
                case 2:
                    return TabThree_Fragment.newInstance();
                default:
                    return TabOne_Fragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            return mNumOfTabs;
        }
    }
}

tabtwo_fragment.xml

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

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">

        <TableRow
            android:id="@+id/row1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/q1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="6dp"
                android:text="Sitting and reading: "
                android:textAppearance="?android:attr/textAppearance" />

            <Spinner
                android:id="@+id/sp1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></Spinner>
        </TableRow>

        <TableRow
            android:id="@+id/row2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/q2"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="6dp"
                android:text="Sitting, inactive in a public place (e.g. a theatre or a meeting): "
                android:textAppearance="?android:attr/textAppearance" />

            <Spinner
                android:id="@+id/sp2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"></Spinner>
        </TableRow>
    </TableLayout>
</RelativeLayout>

的strings.xml

<resources>
    <string name="app_name">PersonalityInsight</string>
    <!-- Custom -->
    <string name="facebook_app_id">XXXXXXXXXXXXXXXX</string>

    <string-array name="ans">
        <item>Zero</item>
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
    </string-array>
</resources>

Error Image

但无论我是从 ARRAY 还是从 XML 文件填充微调器,我都会在此行sp2.setAdapter(adapter);收到错误。我也尝试使用List填充微调器,但发生了同样的错误。

提前致谢。

3 个答案:

答案 0 :(得分:0)

错误表明您的适配器为空。

尝试更改适配器初始化适配器,如下所示:

ArrayAdapter<CharSequence> adapter = 
    new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, R.array.ans);

修改

其实你知道什么,为什么不在你的xml文件中这样做,如下所示:

<Spinner
       android:id="@+id/sp1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:spinnerMode="dropdown"
       android:entries="@array/ans"
       android:prompt="@string/select_one"/>

<强> EDIT2

由于您希望以编程方式执行此操作,请尝试:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.ans)); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
sp2.setAdapter(adapter);
  • R.array.ans已更改为getResources().getStringArray(R.array.ans)

希望这有帮助。

答案 1 :(得分:0)

由于你在你的片段中制作了Spinner,当你试图通过findViewById()在你的活动中找到你的Spinner s1 / s2时,它就不存在了,这就是NPE抛出的。

所以我认为你应该在片段onCreateView()中填充Spinner。

你可以这样做:Error populating spinner in a fragment

答案 2 :(得分:0)

第1步 - &gt;将您的数组添加到string.xml文件

例如

<string-array name="ans">
        <item>Zero</item>
        <item>One</item>
        <item>Two</item>
        <item>Three</item>
    </string-array>

第2步将此数组添加到您的微调器xml

<强>机器人:条目=&#34; @阵列/ ANS&#34;

第3步在您的java文件中

 Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);