Android onItemSelected - NullPointer - android

following code is throwing a NullPointerException:
public class test extends Activity implements OnItemSelectedListener {
private TextView explanation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.psqlpicker);
explanation = (TextView) findViewById(R.id.picker_explanation_text);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.picker_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new test());
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
explanation = (TextView) findViewById(R.id.picker_explanation_text);
}
}
It is thrown because of explanation = (TextView) findViewById(R.id.picker_explanation_text); in the onItemSelected(...) method, but I have no idea why. It is, after all, working in the onCreate(...) method.

Instead of
spinner.setOnItemSelectedListener(new test());
use
spinner.setOnItemSelectedListener(this);
You want to use your real activity as the target; you are creating a new object that is never initialized with a context so when it is called it crashes.

Related

"Error : cannot resolve symbol findViewById"

I'm having a trouble with findViewById but I can't find where the problem is.
Here's my FirstFragment class code:
public class VehicleInformation extends Fragment implements AdapterView.OnItemSelectedListener {
private TextView text_vehicle_no, text_chassis_no;
private TextView text_company_name,text_insurance_id;
RadioGroup radioGroup;
RadioButton radioButton;
private SpinnerAdapter text_vehicle_class;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_vehicle_information, container, false);
return rootView;
radioGroup = (RadioGroup) rootView.findViewById(R.id.insuranceYesNo);
text_vehicle_no = (TextView) rootView.findViewById(R.id.vehicle_no);
text_chassis_no = (TextView) rootView.findViewById(R.id.chassis_no);
Spinner spinner_vehicle_class = (Spinner) rootView.findViewById(R.id.vehicle_class);
Spinner spinner_vehicle_company = (Spinner) rootView.findViewById(R.id.vehicle_company);
Spinner spinner_vehicle_model = (Spinner) rootView.findViewById(R.id.vehicle_model);
Spinner spinner_vehicle_fuel = (Spinner) rootView.findViewById(R.id.vehicle_fuel);
Spinner spinner_vehicle_color = (Spinner) rootView.findViewById(R.id.vehicle_color);
Spinner spinner_vehicle_types = (Spinner) rootView.findViewById(R.id.vehicle_types);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.vehicle_company_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,R.array.vehicle_model_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this,R.array.vehicle_fuel_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this,R.array.vehicle_color_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter7 = ArrayAdapter.createFromResource(this,R.array.vehicle_type_array, android.R.layout.simple_spinner_item);
//specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//aaply the adapter to the spinner
spinner_vehicle_class.setAdapter(adapter);
spinner_vehicle_company.setAdapter(adapter1);
spinner_vehicle_model.setAdapter(adapter2);
spinner_vehicle_fuel.setAdapter(adapter3);
spinner_vehicle_color.setAdapter(adapter4);
spinner_vehicle_types.setAdapter(adapter7);
spinner_vehicle_class.setOnItemSelectedListener(this);
spinner_vehicle_company.setOnItemSelectedListener(this);
spinner_vehicle_model.setOnItemSelectedListener(this);
spinner_vehicle_fuel.setOnItemSelectedListener(this);
spinner_vehicle_color.setOnItemSelectedListener(this);
spinner_vehicle_types.setOnItemSelectedListener(this);
}
public void onStart(){
super.onStart();
EditText datePicker=(EditText) findViewById(R.id.datePicker);
datePicker.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasfocus){
if(hasfocus){
DateDialog dialog=new DateDialog(view);
android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
public void radiobtn(View view) {
int radiobtnselect = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) view.findViewById(radiobtnselect);
Toast.makeText(VehicleInformation.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String sSelected=adapterView.getItemAtPosition(i).toString();
Toast.makeText(VehicleInformation.this,sSelected,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Also showing error on line
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);"
saying wrong 1st argument type
Also showing error on line Toast messsage saying cannot resolve method maketext
Unlike the Activity class, Fragment doesn't have a built-in findViewById() method. This means you need to use View.findViewById(), which you already are doing everywhere except in your onStart() method. Here, you can just change this line:
EditText datePicker=(EditText) findViewById(R.id.datePicker);
to this:
EditText datePicker=(EditText) getView().findViewById(R.id.datePicker);
As for your adapter and toast issues, the problem is that (again, unlike Activity), the Fragment class is not a subclass of Context, so you need to pass a different first argument. Try passing getContext() instead of VehicleInformation.this.

spinner dependent on another spinner in android

http://developer.android.com/resources/tutorials/views/hello-spinner.html
I use this link as the base..
I have 3 spinners, first spinner for country , second for states and third for cities.
I had already seen the links on the stackoverflow , but it is not that much proper.
my code is as follows
public class SpinnerActivity extends Activity
{
String state="";
ArrayAdapter adapter1,adapter2;
Spinner spinner1,spinner2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter1 = ArrayAdapter.createFromResource(
this, R.array.states_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
if(state.equalsIgnoreCase("Gujarat"))
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
if(state == "Rajasthan")
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
state=parent.getItemAtPosition(pos).toString();
Toast.makeText(parent.getContext(), "The state is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
}
the bug I found is , I have created all he thing in the onCreate() method..
but I do not find the alternate thing that can I do ...
I don't know why this not work ..
please help if possible
thanks...
In onSelectItem fill second spinner and so on.

Android Spinners, changing Adapters

Alright, so I'm new to Android programming, so far my experience has been quite interesting and challenging. But I fear I have now encountered the first problem I'm not able to overcome on my own.
Simply put, all I want to do is have 2 Spinners:
1 for country selection
1 for province/state selection
What I want to accomplish is that when the user selects his/her country the province/state Spinner is updated with the correct adapter. Currently I'm only using 2 country for testing purposes.
When I launch the Activity, I get an exception and my app crashes.
Here's my code, any pointers would be appreciated !
public class ManageAccountActivity extends Activity {
final ArrayAdapter<CharSequence> adapterSexe = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.sex_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterProvince = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.province_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterStates = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.state_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.country_array_fr, android.R.layout.simple_spinner_item);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
Spinner spinSexe = (Spinner) findViewById(R.id.spin_sex);
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Spinner spinCountry = (Spinner) findViewById(R.id.spin_country);
adapterSexe.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterStates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinSexe.setAdapter(adapterSexe);
spinProvince.setAdapter(adapterProvince);
spinCountry.setAdapter(adapterCountry);
spinCountry.setOnItemSelectedListener(new CountryOnItemSelectedListener());
}
public class CountryOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
setContentView(R.layout.account_management);
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Here's the LogCat message I'm getting.
01-10 20:41:01.024: E/AndroidRuntime(1275):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{gggolf.android.minutegolf/gggolf.android.minutegolf.ManageAccountActivity}:
java.lang.NullPointerException
You cannot instantiate your ArrayAdapter directly on class attribut, because createFromResource() use Context, and it's not exists at this time, do it in onCreate() methode instead.
In addition, you get spin province wrong in your listener, you can't call findViewById on view local variable, because it's not your layout, but an inflate of android.R.layout.simple_spinner_item
The good way:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
Try having your Activity implement AdapterView.OnItemSelectedListener itself. Notice I've moved the Spinners and left out some of your code and replaced it with comments - make sure you include it where necessary...
public class ManageAccountActivity extends Activity
implements AdapterView.OnItemSelectedListener {
// Your ArrayAdapters as before
Spinner spinSexe = null;
Spinner spinProvince = null;
Spinner spinCountry = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
spinSexe = (Spinner) findViewById(R.id.spin_sex);
spinProvince = (Spinner) findViewById(R.id.spin_province);
spinCountry = (Spinner) findViewById(R.id.spin_country);
// Call setDropDownViewResource on your ArrayAdapters
// Call setAdapter on your Spinners
spinCountry.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
}
}
Why do you have setContentView(R.layout.account_management); in your onItemSelected() method? That should not be necessary.
Furthermore, you should instantiate your adapters in the onCreate() method of your activity, and pass your actual activity instance as the context.
And the code for retrieving the Spinner object in the select listener should be changed from
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
into
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Calling findViewById() on the local view object in your select listener will return NULL because the view does not contain the Spinner.

Android: Changing a textview with in a different Activity

I am trying to change textview properties of a textview that looks like this:
In a seperate Activity that looks like this:
I tried to do this with bundles but I can't get it to work.
This is how my BookActivity looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
//this is where the size property comes in
Integer size = getIntent().getExtras().getInt("SGRkey");
TextView test2 = (TextView) findViewById(R.id.booktext);
test2.setTextSize(size);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] theporn = getResources().getStringArray(R.array.allkapitel);
TextView text = (TextView) findViewById(R.id.booktext);
text.setText(theporn[pos]);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
(i pick the chapter string in the spinner and that works just fine.)
And this is how my SettingsActivity looks like:
public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SGRspinner.setAdapter(SGRadapter);
}
public class SGROnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Intent answer = new Intent();
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
int SGRint = Integer.parseInt(SGRstring[pos]);
Bundle size = new Bundle();
size.putInt("SGRkey", SGRint);
Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
nextActivity.putExtras(size);
com.asm.reader.SettingsActivity.this.startActivity(nextActivity);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I get an error when I try this. All activities are declared in the manifest. I really don't know how to go on. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
Make your textview static. That is, declare it as a public static class variable. Then you can call it directly from the other activity like this: firstActivity.myTextView.setText("foo");

how to add item to Spinner's ArrayAdapter?

i had a EditText , a button and a spinner . When click the button , the spinner will add a new item with name you entered in the EditText. But here is the question, my adapter.add() method seems doesn't work...here is my code:
public class Spr extends Activity {
Button bt1;
EditText et;
ArrayAdapter<CharSequence> adapter;
Spinner spinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)this.findViewById(R.id.bt1);
et = (EditText)this.findViewById(R.id.et);
spinner = (Spinner)this.findViewById(R.id.spr);
adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String temp = et.getText().toString();
adapter.add(temp);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
}
});
spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
}
}
thanks! ...still waitting
When you have created your ArrayAdapter you haven't assigned a resizeable List to it, so when you do add() it cannot increment the size of it and throws a UnsupportedOperationException.
Try something like this:
List<CharSequence> planets = new ArrayList<CharSequence>();
adapter = new ArrayAdapter<CharSequence>(context,
R.array.planets_array, planets);
//now you can call adapter.add()
You should use a List. With an Array such as CharSequence[] you would get the same UnsupportedOperationException exception.
Javi is right except don't reference an array for the second parameter.
adapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,
someList);
I believe this is working as designed, but not as expected. ArrayAdapter used to only take an array, but the list constructor was added later. I'm guessing its just doing a toArray() on your list. This is why you have to either call add on the adapter, or create a new adapter when your List changes.
you can create an arraylist and copy all recourse to this object then create arrayadaptor and send this arraylist and in onclicklistener of button, add edittext content to arraylist object then call notifydatasetchanged of adator

Categories

Resources