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!
Related
I have extended the spinner class in order to customize it, but i can't figure out how to use it in my code.
public class mySpinner extends Spinner {
......
}
I have an existing activity that uses a spinner and I want to change it to use mySpinner class.
Originally:
Spinner spinner = (Spinner) textEntryView
.findViewById(R.id.account_spinner);
and
<Spinner
android:id="#+id/account_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/ADALabel1"
android:layout_alignLeft="#+id/newassetsymbol"
android:layout_below="#+id/assetclass_spinner" />
first I tried:
mySpinner spinner = (Spinner) textEntryView
.findViewById(R.id.account_spinner);
compile error (makes sense).
then I tried:
mySpinner spinner = (mySpinner) textEntryView
.findViewById(R.id.account_spinner);
runtime cast error.
Then I changed the layout with the previous line:
<mySpinner
android:id="#+id/account_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/ADALabel1"
android:layout_alignLeft="#+id/newassetsymbol"
android:layout_below="#+id/assetclass_spinner" />
01-12 06:50:02.664: E/AndroidRuntime(597): Caused by: java.lang.ClassNotFoundException: android.view.mySpinner in loader dalvik.system.PathClassLoader[/data/app/org.sample.sample-1.apk]
This does not seem thst hard but I am missing something. Can someone help?
Thank you.
When using custom View components in XML, you need to use the complete class path.
<com.example.appname.mySpinner />
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.. :)
I have a list of "categories" that are stored as Strings in an ArrayAdapter in my app. This much is pretty simple. The adapter is a field of the Activity and accessible everywhere. It is populated with values during onCreate().
I have a "Entry" Dialog that contains an AutoCompleteTextView which uses this adapter and works very well. This is basically a dialog to add new items to a list.
I also have a second Dialog which acts as a filter for my list and has a single Spinner which when the Dialog is created, uses the same ArrayAdapter. Here's the problem.
If I use the filter Dialog before the entry Dialog, the Spinner is populated with all of the items from the adapter. Works well.
Any and every time I use the entry Dialog, the AutoCompleteTextView works properly.
The problem comes if I use the entry Dialog and select an item in the AutoCompleteTextView when it suggests something. After selecting a suggested item in the popup, even if I cancel the entry Dialog, the next time I bring up the filter Dialog, the Spinner initially shows the item that was last selected from the AutoCompleteTextView (instead of the first item in the adapter), and only displays that single item in the Spinner's list if clicked/touched. The only way to fix it is to end the application and re-open it. I'm not getting any errors or anything in logcat that is helpful.
EDIT - Okay, I've removed the previous code to replace it with a simple test case that I produced. The bottom line is that I would like to know if this is a bug, or if if it is expected results. When a suggestion is selected in an AutoCompleteTextView, I can confirm that the ArrayAdapter that is linked to it has filtering applied so that it's count is affected and the only items shown if the adapter is accessed in a Spinner will be those that were filtered down to. I also added button to display a toast to show that the count is affected. Type "te" in the autocomplete, select either Test entry. They try the spinner or click the button to see the adapter's count is only 2.
So the final question is now... can the adapter's filter be reset (other than by typing in and clearing the AutoCompleteTextView)? I can't find any method to do this. I have worked around the problem in my actual app by setting up a temporary adapter, copying over the main adapters items and setting the views to use the temporary adapter instead. My phone is running 2.2, and I have tested as high as 2.3.3 API level 10 in an emulator.
The xml for main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AutoCompleteTextView android:id="#+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check It"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" />
</RelativeLayout>
The code for MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView actv;
Spinner spinner;
Button button;
String [] adapterList = {"Test1", "Test2", "Garbage1", "Garbage2"};
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, adapterList);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
spinner = (Spinner) findViewById(R.id.spinner);
button = (Button) findViewById(R.id.button);
actv.setAdapter(adapter);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + adapter.getCount() + " Items in Adapter", Toast.LENGTH_SHORT).show();
}
});
}
}
Looking at AutoCompleteTextView source code, it does filter the ArrayAdapter:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29
You may want to remove the filter on an onClick callback in the spinner:
Filter filter = adapter.getFilter();
filter = null;
(see the setAdapter method in AutoCompleteTextView, around line 600)
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29
Let me know if it works
BTW, is the adapter going to change dynamically? You're "workaround" of two adapters might be a better option than messing with the filter. What if the user starts a word, then clicks the spinner, cancels it and goes back to the word? Now the autocompletion will not make sense, unless you save the filter and restore it somehow.
I do believe that two adapters (based on the same string array) is a better solution.
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.
Having a problem loading an array into a spinner that is located on a different view. The array is defined properly in arrays.xml with a name of beerstyles. The beerstylespinner is defined as the id of a spinner in carbonationcalculator_view.xml. This works when the code is in the main java class but not the additional carbonationcalculator class. Everything works with the exception of the spinner not being populated with the array.
Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.carbonationcalculator_view);
Spinner s = (Spinner) findViewById(R.id.beerstylespinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.beerstyles, R.layout.carbonationcalculator_view);
adapter.setDropDownViewResource(R.layout.carbonationcalculator_view);
s.setAdapter(adapter);
}
I'm not sure this will fix the problem, but there seems to be some confusion on what layout to set for the Adapter's drop down resource. setContentView() should be used for the view you want set for the activity; however, the drop down resource should be what you want each row to look like.
What you should be using is a something like android.R.layout.simple_list_item_1. You can emulate the demo List1.java, but instead of their constructor you would use:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.beerstyles, android.R.layout.simple_list_item_1);
Also, you don't need to call ArrayAdapter.setDropDownViewResource() after using ArrayAdapter.createFromResource() - the third parameter is the drop down view resource.