java – 设置setOnItemSelectedListener时的Spinner错误

前端之家收集整理的这篇文章主要介绍了java – 设置setOnItemSelectedListener时的Spinner错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我使用spinner下拉列表或setOnItemSelectedListener时出现错误

nav_login.xml:

strinarray:

主要活动:

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


    Firebase.setAndroidContext(this);
    Firebase rootRef = new Firebase("https://intense-heat-4844.firebaseio.com/property");

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    View mLoginButton = navigationView.inflateHeaderView(R.layout.nav_header);
    mLoginButton.findViewById(R.id.Login);
    if (!myBooleanVariable) {
        mLoginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Calling activity LoginRegister
                Intent intent = new Intent(getApplication(),LoginRegister.class);
                startActivity(intent);
            }
        });
    }

    Intent intent = getIntent();
    if (null != intent) {
        myBooleanVariable = intent.getBooleanExtra("my_boolean_key",myBooleanVariable);
        if (myBooleanVariable) {
            navigationView.getHeaderView(0).setVisibility(View.GONE);
            navigationView = (NavigationView) findViewById(R.id.nav_view);
            View mUserLoggedIn = navigationView.inflateHeaderView(R.layout.nav_header_login);


            final Spinner spinner;
            spinner = (Spinner) findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView

例外:

03-01 13:17:47.039    8587-8587/com.example.syedsaadali.myapplication I/System.out﹕ User ID: 4310e794-0b9b-4fad-afd0-e3f73c5f7412,Provider: password
03-01 13:17:47.074    8587-8587/com.example.syedsaadali.myapplication D/AndroidRuntime﹕ Shutting down VM
03-01 13:17:47.074    8587-8587/com.example.syedsaadali.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.syedsaadali.myapplication,PID: 8587
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.syedsaadali.myapplication/com.example.syedsaadali.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
            at com.example.syedsaadali.myapplication.MainActivity.onCreate(MainActivity.java:72)
最佳答案
如果你这样使用,
spinner =(Spinner)findViewById(R.id.spinner);
它将在布局activity_main中搜索微调器(因为您将activity_main设置为onCreate中的内容视图),但它不是activity_main的子级.所以它抛出NullPointerException.

而是像这样使用,

spinner =(Spinner)mUserLoggedIn.findViewById(R.id.spinner);
(如果在nav_header_login布局中有微调器)

原文链接:https://www.f2er.com/android/429951.html

猜你在找的Android相关文章