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 :");
精彩评论