简体   繁体   中英

Spinner on android studio

its a very simple code yet it cant run

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Categories, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
itemCat.setAdapter(adapter);

Ive named my spinner itemCat, array as Categories and adapter as adapter.

Process: com.example.myapplication, PID: 3716 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.item_register}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

You have to first create the view then call the methods.

Secondly, the spinner should be created under the onCreate() .

某些东西是空的,导致这次崩溃。我猜你的 itemCat 是空的,你没有创建它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM