Это мой первый вопрос, так что не убивайте меня.
Я пытался использовать спиннер для приложения.Фактически это сработало несколько недель назад.После переустановки Android Studio я просто получаю NPE для этого: /
log:
> 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, чего он не делает.