Android Spinner主要活动问题

时间:2019-03-01 14:53:53

标签: android

我想在应用程序的主活动上添加微调器(图中的红色圆圈所示)。旋转器将用于血型拾取。我使用了微调器的代码,该代码已经在另一个微调器中用于其他活动中的血型选择。另一项活动是要注册表格。那里很棒。因此,我只是复制并粘贴代码而没有错误。但是之后,当我运行该应用程序时,它只是在开始时就崩溃了,并且无法打开。我做错了什么?

screenshot

package com.sarkerjr.greenBlood;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.sarkerjr.greenBlood.data.BloodContract;

public class MainActivity extends AppCompatActivity {

    //Spinner for selecting blood group on search
    private Spinner mBloodTypeSpinner;

    private int mBloodType;


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

        // Setup FAB to open EditorActivity
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, EditorActivity.class);
                startActivity(intent);
            }
        });

        mBloodTypeSpinner = (Spinner) findViewById(R.id.spinner_bloodType);

        //Set the blood picker spinner
        setupBloodTypeSpinner();
    }


    /**Setup spinner for blood type pickup*/

    private void setupBloodTypeSpinner() {
        // Create adapter for spinner. The list options are from the String array it will use
        // the spinner will use the default layout
        ArrayAdapter bloodTypeSpinnerAdapter = ArrayAdapter.createFromResource(this,
                R.array.array_bloodType_options, android.R.layout.simple_spinner_item);

        // Specify dropdown layout style - simple list view with 1 item per line
        bloodTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        // Apply the adapter to the spinner
        mBloodTypeSpinner.setAdapter(bloodTypeSpinnerAdapter);

        // Set the integer mSelected to the constant values
        mBloodTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selection = (String) parent.getItemAtPosition(position);
                if (!TextUtils.isEmpty(selection)) {
                    if (selection.equals(getString(R.string.a_positive))) {
                        mBloodType = BloodContract.DonorEntry.A_Positive;
                    } else if (selection.equals(getString(R.string.a_negative))) {
                        mBloodType = BloodContract.DonorEntry.A_Negative;
                    } else if (selection.equals(getString(R.string.b_positive))) {
                        mBloodType = BloodContract.DonorEntry.B_Positive;
                    }else if (selection.equals(getString(R.string.b_negative))) {
                        mBloodType = BloodContract.DonorEntry.B_Negative;
                    }else if (selection.equals(getString(R.string.o_positive))) {
                        mBloodType = BloodContract.DonorEntry.O_Positive;
                    }else if (selection.equals(getString(R.string.o_negative))) {
                        mBloodType = BloodContract.DonorEntry.O_Negative;
                    }else if (selection.equals(getString(R.string.ab_positive))) {
                        mBloodType = BloodContract.DonorEntry.AB_Positive;
                    }else if (selection.equals(getString(R.string.ab_negative))) {
                        mBloodType = BloodContract.DonorEntry.AB_Negative;
                    } else{
                        mBloodType = BloodContract.DonorEntry.TYPE_UNKNOWN;
                    }
                }
            }

            // Because AdapterView is an abstract class, onNothingSelected must be defined
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                mBloodType = BloodContract.DonorEntry.TYPE_UNKNOWN;
            }
        });
    }
}

这是堆栈跟踪报告(尽管我听不懂)。

2019-03-01 21:06:32.417 23087-23087/com.sarkerjr.greenBlood E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sarkerjr.greenBlood, PID: 23087
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sarkerjr.greenBlood/com.sarkerjr.greenBlood.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
        at com.sarkerjr.greenBlood.MainActivity.setupBloodTypeSpinner(MainActivity.java:57)
        at com.sarkerjr.greenBlood.MainActivity.onCreate(MainActivity.java:41)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

1 个答案:

答案 0 :(得分:2)

您的微调框似乎为空(未正确初始化)

检查布局文件中引用的ID是否正确。