开发者

Null PointerException in spinner in android

开发者 https://www.devze.com 2023-04-12 22:51 出处:网络
I am working in Android. I want to design a spinner of song categories. This is my code: public Spinner spinner_category_forSong;

I am working in Android. I want to design a spinner of song categories.

This is my code:

public Spinner spinner_category_forSong;

String[] arr_Category={"Select","sad","dj","rock"};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
    this,android.R.layout.simple_spinner_item,arr_Category);

spinner_category_forSong = (Spinner)findViewById(R.id.Spinner_category_forUpload);    
spinner_ca开发者_运维百科tegory_forSong.setPrompt("Music Category :");
spinner_category_forSong.setAdapter(adapter);

But whenever I run my project, a null pointer exception is created in spinner_category_forSong.setPrompt("Music Category :"); and spinner_category_forSong.setAdapter(adapter);.

Please tell me what mistake I have made in this code.


From your exception it seems that findViewById(R.id.Spinner_category_forUpload) returns null. My guess is that you didn't call setContentView() prior to your method calls.

From the code sample your class structure is not clear. Are some lines inside onCreate()?


try after setting adapter spinner_category_forSong.setAdapter(adapter); before spinner_category_forSong.setPrompt("Music Category :");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号