Solve the problem that the Spinner cannot use it in Tabhost.

Source: Internet
Author: User

I wanted to write this article last night, but my school's network is really bad! This page cannot be opened !!!
Let's talk about the text:
Program Background: to use TabHost, you need to add the Spinner button in the subinterface.
Q: You cannot click the "Spinner" button. The "unable to add window... is your activity running" button displayed? .
Exploration:
After you click the Spinner, a dropdown item tab will pop up. I think the unable to add window should be an activity that cannot be added to this tab. The last sentence of the error shows that the activity that the program thinks it needs is not running. Which activity does the Spinner need? Which activity is currently running?
I did not want to understand it after thinking about it. I found some information on the Internet. In a blog, AlertDialog. Builder (xxx. this) => AlertDialog. Builder (this. getParent ()). I tried it according to this. If this. getParent () is not added, the above error will occur. If this is added, no problem will occur. It can be seen that the current activity should be tabhost, rather than the activity corresponding to each tab.
Again, where should I change the current activity to this. getParent () for the Spinner ()? I used findViewById (...) in the program. A simple method is to directly use new Spinner (Context context, AttributeSet attributeSet). The context here uses this. getParent () to be the line. However, I am too layout to use AttributeSet.
After another night, it seems that some people on the Internet say that setContentView (R. layout...) cannot be used ...). I suddenly realized that this guy must have been a ghost. It generated a Spinner. So I changed the code to the following:
View contentView = LayoutInflater. from (this. getParent (). inflate (R. layout. search_activity, null );
// SetContentView (R. layout. search_activity );
SetContentView (contentView );
Now the problem is finally solved... I can only say that this issue cannot be avoided without looking at the underlying implementation code of android. (This problem has plagued me for a long time, but it has been around before ).

The above red code is the information I found online;
I tried it again according to the method mentioned above. If the Spinner in your program is not in the Dialog, you can use
View contentView = LayoutInflater. from (this. getParent (). inflate (R. layout. search_activity, null );
// SetContentView (R. layout. search_activity );
SetContentView (contentView );
Method to Solve 1, no problem.

However, I want to use the Spinner in the Dialog, which is not useful as described above. Maybe I don't know enough about it.

At that time, I thought it could not be a problem with the layout file of android layout XML. If I use java code to write a Dialog box (including the Spinner and EditText components ).

The Code is as follows:

RelativeLayout myCityRelativeLayout = null;
TextView myProvinceTextView = null;
Spinner myProvinceSpinner = null;
ArrayAdapter <String> myProvinceArrayAdapter = null;
ArrayList <String> provinces = null;
RelativeLayout. LayoutParams myProvinceTextViewLP = null;
Cursor cursor = null;
Final Spinner spinner = null;

Switch (item. getItemId ()){
Case MENU_ADD:

MyCityRelativeLayout = new RelativeLayout (CityManagerActivity. this );
// Set the relative layout width and height
MyCityRelativeLayout. setLayoutParams (new RelativeLayout. LayoutParams (ViewGroup. LayoutParams. WRAP_CONTENT, ViewGroup. LayoutParams. WRAP_CONTENT ));
// Set the components in the relative layout to align to the left
MyCityRelativeLayout. setGravity (Gravity. LEFT );

// Define the TextView object myProvinceTextView, Number 1, and content as Province
MyProvinceTextView = new TextView (CityManagerActivity. this );
MyProvinceTextView. setId (1 );
MyProvinceTextView. setText (R. string. province );
// Define the myProvinceTextViewLP object. Layout: it is the top layout, alignment to the left of the parent class, And 10px from the left.
MyProvinceTextViewLP = new RelativeLayout. LayoutParams (ViewGroup. LayoutParams. WRAP_CONTENT, ViewGroup. LayoutParams. WRAP_CONTENT );
MyProvinceTextViewLP. addRule (RelativeLayout. ALIGN_PARENT_TOP );
MyProvinceTextViewLP. addRule (RelativeLayout. ALIGN_PARENT_LEFT, RelativeLayout. TRUE );
MyProvinceTextViewLP. setMargins (10, 0, 10, 0 );
MyCityRelativeLayout. addView (myProvinceTextView, myProvinceTextViewLP );

// Define the myProvinceSpinner object and the ID is 2
MyProvinceSpinner = new Spinner (CityManagerActivity. this );

Cursor = DBHelper. getInstance (CityManagerActivity. this). selectProvince ();
MyProvinceArrayAdapter = new ArrayAdapter <String> (CityManagerActivity. this, android. R. layout. simple_spinner_item );
MyProvinceArrayAdapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );

While (cursor. moveToNext ())
{
System. out. println (cursor. getString (1 ));
MyProvinceArrayAdapter. add (cursor. getString (1 ));
}


MyProvinceSpinner. setAdapter (myProvinceArrayAdapter );
MyProvinceSpinner. setId (2 );
ProvinceName = myProvinceSpinner. getSelectedItem (). toString ();
// System. out. println (provinceName );
MyProvinceSpinner. setOnItemSelectedListener (myProvinceSpinnerListener );
// Define the myProvinceSpinnerLP object. Layout: it is the top layout, alignment to the left of the parent class, 10px to the left, and 10px to the right.
RelativeLayout. LayoutParams myProvinceSpinnerLP = new RelativeLayout. LayoutParams (ViewGroup. LayoutParams. FILL_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT );
MyProvinceSpinnerLP. addRule (RelativeLayout. ALIGN_PARENT_TOP );
MyProvinceSpinnerLP. addRule (RelativeLayout. RIGHT_OF, myProvinceTextView. getId ());
MyProvinceSpinnerLP. setMargins (10, 0, 10, 0 );
MyCityRelativeLayout. addView (myProvinceSpinner, myProvinceSpinnerLP );

// Define the myCityTextView object, no.: 3. Content: City
TextView myCityTextView = new TextView (CityManagerActivity. this );
MyCityTextView. setText (R. string. city );
MyCityTextView. setId (3 );
// Define the myCityTextViewLP object. Layout: align with the myProvinceTextView component on the left, below the myProvinceTextView component, 10px from the left, and 10px from the right
RelativeLayout. LayoutParams myCityTextViewLP = new RelativeLayout. LayoutParams (ViewGroup. LayoutParams. WRAP_CONTENT, ViewGroup. LayoutParams. WRAP_CONTENT );
MyCityTextViewLP. addRule (RelativeLayout. ALIGN_PARENT_LEFT, RelativeLayout. TRUE );
MyCityTextViewLP. addRule (RelativeLayout. BELOW, myProvinceSpinner. getId ());
MyCityTextViewLP. setMargins (10, 0, 10, 0 );
MyCityRelativeLayout. addView (myCityTextView, myCityTextViewLP );

// Define the myCityEditText object. ID: 4. Content: Click the content in the list.
Final EditText myCityEditText_add = new EditText (CityManagerActivity. this );


// Define the myCityEditTextLP object. Layout: align with the myCityTextViewLP component on the left, below the myCityTextViewLP component, 10px from the left, and 10px from the right
RelativeLayout. LayoutParams myCityEditTextLP = new RelativeLayout. LayoutParams (ViewGroup. LayoutParams. FILL_PARENT, ViewGroup. LayoutParams. WRAP_CONTENT );
MyCityEditTextLP. addRule (RelativeLayout. BELOW, myProvinceSpinner. getId ());
MyCityEditTextLP. addRule (RelativeLayout. RIGHT_OF, myCityTextView. getId ());
MyCityEditTextLP. addRule (RelativeLayout. ALIGN_PARENT_RIGHT, RelativeLayout. TRUE );
MyCityEditTextLP. setMargins (10, 0, 10, 0 );
MyCityRelativeLayout. addView (myCityEditText_add, myCityEditTextLP );

// Define the dialog box www.2cto.com
New AlertDialog. Builder (CityManagerActivity. this)
. SetView (myCityRelativeLayout)
. SetIcon (R. drawable. add)
. SetTitle (R. string. add_city)
. SetPositiveButton (R. string. add, new OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog, int which ){

String cityName = myCityEditText_add.getText (). toString ();
If (cityName = null | cityName. trim (). length () = 0)
{
Toast. makeText (CityManagerActivity. this, "the city cannot be blank !!! ", Toast. LENGTH_LONG );
} Else {
Integer provinceId = null;
If (provinceName! = Null)
{
Cursor cursor = DBHelper. getInstance (CityManagerActivity. this). selectProvinceByName (provinceName );
While (cursor. moveToNext ())
{
ProvinceId = cursor. getInt (0 );

}
}
Province province = new Province ();
Province. setId (provinceId );

City city = new City ();
City. setName (cityName );
City. setProvince (province );

DBHelper. getInstance (CityManagerActivity. this). addCity (city );

}



}
})
. SetNegativeButton (R. string. cancel, null)
. Show ();
Break;

When I run this project again, I finally achieved the expected results !!! I'm so happy !!!

This problem has plagued me for several hours. At the beginning, I thought there was a problem with the code of the Spinner. I did not solve it with various changes. I began to doubt my abilities !!!

I have learned from this question:

1: Do not rely too much on the layout file of android. Sometimes you can manually write the layout code;

2: If you do not know about android Samples or the source code;

3: when a problem is found, check the console for an exception and then search for the key part on goolge;

4: The Never give up method is always more problematic, so you have to calm down and solve it slowly. Now that you choose this line, you have to stick to it.

 

From stark-summer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.