NPE 无效 android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)

NPE with void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)

这是我的第一个问题,所以不要杀了我。

我尝试为应用程序使用微调器。 事实上,这在几周前就奏效了。 重新安装 android studio 后,我得到了一个 NPE:/

日志:

> E/AndroidRuntime: FATAL EXCEPTION: main
>     Process: pxnx.rrcompanion, PID: 31117
>     java.lang.RuntimeException: Unable to start activity ComponentInfo{pxnx.rrcompanion/pxnx.rrcompanion.BuildingCostActivity}:
> 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:2984)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
>         at android.app.ActivityThread.-wrap14(ActivityThread.java)
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
>         at android.os.Handler.dispatchMessage(Handler.java:102)
>         at android.os.Looper.loop(Looper.java:154)
>         at android.app.ActivityThread.main(ActivityThread.java:6776)
>         at java.lang.reflect.Method.invoke(Native Method)
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
>      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
> android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a
> null object reference
>         at pxnx.rrcompanion.BuildingCostActivity.onCreate(BuildingCostActivity.java:57)
>         at android.app.Activity.performCreate(Activity.java:6956)
>         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
>         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
>         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
>         at android.app.ActivityThread.-wrap14(ActivityThread.java) 
>         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
>         at android.os.Handler.dispatchMessage(Handler.java:102) 
>         at android.os.Looper.loop(Looper.java:154) 
>         at android.app.ActivityThread.main(ActivityThread.java:6776) 
>         at java.lang.reflect.Method.invoke(Native Method) 
>         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) 
>         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) 
> Application terminated.

BuildingCostActivity.java

class BuildingCostActivity extends pxnx.rrcompanion.MainActivity {

    Spinner sp_BuildingSelect;
    EditText et_beginBC, et_endBC;
    Button btn_calcBC;
    TextView tv_moneyBC_res, tv_goldBC_res, tv_oilBC_res, tv_oreBC_res, tv_diamondBC_res, tv_uraniumBC_res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      
        View contentView = inflater.inflate(R.layout.buildingcost, null, false);

        sp_BuildingSelect = (Spinner) findViewById(R.id.BuildingSelect);

      String[] buildingtypes = new String[]{
                "Hospital",
                "Military Base",
                "School",
                "Missilesystem",
                "Seaport",
                "Powerplant",
                "Spaceport",
                "Airport",
                "Housefund"
        };

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this,R.layout.spinneritem,buildingtypes
        );
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinneritem);
        sp_BuildingSelect.setAdapter(spinnerArrayAdapter); 
             //that is where log leads me to

main.xml

<Spinner
            android:id="@+id/BuildingSelect"
            style="@android:style/Widget.DeviceDefault.Light.Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorAccent"
            android:dropDownWidth="match_parent"
            android:minWidth="224dp"/>

它应该 - 如您所见 - 使用 String 中的项目创建一个微调器,但它没有。

您忘记分配 sp_BuildingSelect。例如,

sp_BuildingSelect = (Spinner) findViewById(R.id.BuildingSelect); 

你没有正确地膨胀你的布局。这将起作用:

super.onCreate(savedInstanceState);
setContentView(R.layout.buildingcost);

sp_BuildingSelect = (Spinner) findViewById(R.id.BuildingSelect);

String[] buildingtypes = new String[]{
     "Hospital",
     "Military Base",
     "School",
     "Missilesystem",
     "Seaport",
     "Powerplant",
     "Spaceport",
     "Airport",
     "Housefund"
 };

 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
     this,R.layout.spinneritem,buildingtypes
 );
 spinnerArrayAdapter.setDropDownViewResource(R.layout.spinneritem);
 sp_BuildingSelect.setAdapter(spinnerArrayAdapter); 

发生的事情是 class 布局正在加载但未分配给您的 Activity 并且 findViewById 您试图在空布局中找到它。