Problem in Android Spinner inside the Tabbars - android

I am making an application and i am facing a problem.
I am using Tab bars and in one of my Tab bar i am using Spinner.
It loads Perfectly but when i click on it. it gives me:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#44647ef8 is not valid; is your activity running?
This error.
i am using the following code snippet
ArrayList<String> ageList;
Spinner age;
age = (Spinner) findViewById(R.id.country);
ageList = new ArrayList<String>();
ageList.add("10-20");
ageList.add("21-35");
ageList.add("36-60");
ageList.add("61-100");
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ageList); //array you are populating
adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
age.setAdapter(adapter2);
age.setSelection(0, true);
This code work fine when i am using seperate activity not the tab bars. but in tab bars it gives me the above error exception when i click on the spinner to open a list.
Please guide me Thanks a bunch

I m done with this
the problem was with my layout
setContentView(R.layout.age)
Instead of this i used Layout Inflator as below:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.country, null);
this.setContentView(viewToLoad);
and then called the Spinner n Bingooo! It works Just Perfect
Thanks all of you.. :)

Related

how to disable spinner click? setclickable(false) not working

i want to disable dropdown list of spinner if it have only one item, i have almost tried everything but still i didn't get the solution, some times i got nullpointerexception while trying any solution, here is my code.
ArrayAdapter<String> adapter =new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,str_fname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_childName.setAdapter(adapter);
if(str_fname.length==1){
sp_childName.setBackground(null);
sp_childName.setClickable(false);
tv_childClassname.setText(str_className[0]);
}else{
sp_childName.setOnItemSelectedListener(new onItemSelected());
}

Problems changing the background colors in an android listview without clicking

When my application loads, I need some(not necessarily all) of the items in my list view to change their backgrounds without clicking on the ones I need. I know the method to change background, but I don't know how to extract the views from the ListView to do that.
I looked at this question and the answer did not work:
https://stackoverflow.com/questions/10650692/how-to-change-background-color-list-view-item-without-click-on-any-of-them
Here is some of my code:
mApprovalList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.approvals_array)));
mAdapter = new ArrayAdapter<String>(this,
R.layout.approvals_list,
immApprovalList);
setListAdapter(mAdapter);
ListView lv = getListView();
Log.v(String.valueOf(lv.getChildCount()), "count");
Also, that "log" returns 0.
Thanks in advance!
You should write a custom adapter and have it set the backgrounds to whatever color you'd like. See https://stackoverflow.com/a/5561813/458968
Then use
MyCustomAdapter customAdapter = new MyCustomAdapter(this, mApprovalList);
setListAdapter(customAdapter);
in your onCreate method.

Listview and dialog in android

I have a small custom dialog in wich I have a ListView with id:list. I want to populate it with strings I have in my resources (R.array.tones), but I have really big problems making this work, have tried many different solutions this is the latest which I think would work but it throws an null pointer exception on the toneList.
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.tone_dialog);
dialog.setTitle(R.string.tonePromptTitle);
ListView toneList = (ListView)findViewById(R.id.list);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tones, android.R.layout.simple_list_item_1);
toneList.setAdapter(adapter);
dialog.show();
My class is just extending Activity not ListActivity, and I would like to have it that way otherwise I must create a new class just for the listview.
I apologize for the long code, I'm new here and hasn't really figured out all functionality yet.
You should look for the View inside of the dialog:
ListView toneList = (ListView)dialog.findViewById(R.id.list);
:)
You are not searching your dialog layout for toneList. Try the following instead:
ListView toneList = (ListView)dialog.findViewById(R.id.list);

Error while placing a spinner inside Activity Group

I have an activity group containing 3 activities. When a button is pressed, I enter into this activity group and show the 1st activity. From the 1st activity I can goto 2nd activity and from 2nd activity I can goto 3rd activity.
I have a spinner in this 3rd activity layout. Problem is I am not able to click on that spinner. Error gets displayed showing:
12-31 11:29:41.082: ERROR/AndroidRuntime(474): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord#43791b18 is not valid; is your activity running?
How can I solve this issue? Can anyone plz help...
Hi,
Please find the code for spinner attached:
setContentView(R.layout.requestinfo);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
PGDealerInfoRequestActivity.this, R.array.request_options, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
Inside requestinfo.xml,
<Spinner android:id="#+id/spinner" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#android:color/darker_gray"
android:textSize="12sp" android:textStyle="bold"
android:layout_marginLeft="10dp" android:layout_marginTop="8dp" />
The error was with the setContentView.
I had given
setContentView(R.layout.mylayout);
Instead of that we should give,
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.mylayout, null);
this.setContentView(viewToLoad);
And the spinner code is:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.request_options, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
can you add spiner.setDropDownViewResource()?and in your initialization ,you use context called this or getApplicationContext(),for example AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent())
this may solve your problem
this.getParent()
i used it in my code many times. it worked f9.
Mathew his method works:
View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.mylayout, null);
this.setContentView(viewToLoad);
i folowed this method to and then my application crashes on button click.
To solve this remove the onClick method out the xml file.
Go to the java class and add button.setOnClickListener!

Problems with creating spinner

In my app I'm developing, I have a spinner in an alert dialog box.
The spinner works fine, but when I added the following lines to add the array to the spinner, my app crashing a few seconds after starting up:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.newFileTypeArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
newFileType.setAdapter(adapter);
What am I doing wrong?
These Android spinners seem to be a bit complicated as well, I don't think I'll be able to remember how to make them without referencing to the Android docs.
Problem solved.
I realised that the following line:
final Spinner newfiletypespinner = (Spinner) findViewById(R.id.newfiletypespinner);
I had to change to:
final Spinner newfiletypespinner = (Spinner) newFileDialogInflated.findViewById(R.id.newfiletypespinner);
With "newFileDialogInflated" being the previously inflated view so I could have a custom AlertDialog view:
final View newFileDialogInflated = View.inflate(this, R.layout.newfileview, null);
But thanks for the help!
Hard to say for sure whether you've omitted it from your snippet or omitted it from your code, but do you initialize newFileType as a spinner?
Spinner newFileType = (Spinner)findViewById(R.id.newFileTypeSpinner);
or similar? If you're trying to set its adapter before initializing it, that would explain it.

Categories

Resources