AutoCompleteTextView NullPointer Exception - android

I have searched a long time and cannot find a solution to my problem. I am trying to create a Dialog with an AutoCompleteTextView. I followed the tutorial on the Android developer website, and it worked great. I have been successful using layouts on Dialogs before, so I thought this would be just as easy. I created a layout for my Dialog and I made sure that the AutoCompleteTextView has an ID. Here's where the interesting stuff happens...
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)findViewById(R.id.role_ac);
Here is the layout as well.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Role" />
<AutoCompleteTextView android:id="#+id/role_ac" android:layout_width="280dip" android:layout_height="wrap_content"/>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="Done"
android:id="#+id/auto_doneButton" />
</LinearLayout>
For some reason, auto_tv is null even though it does exist in the layout auto_layout. The only way I have been able to get an AutoCompleteTextView in a Dialog is by building the layout programmatically. Why is the AutoCompletTextView null when I try to use it? Did I forget something in my layout or am I not constructing the object correctly? Any help on this would be greatly appreciated. Thanks.

You're mixing two different contexts in your calls to setContentView and findViewById. In the first statement you're setting the content view of the object dialog. In the second statement you're looking for an view in the parent activity. You want to make both calls using the dialog object. The correct syntax should be:
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView) dialog.findViewById(R.id.role_ac);

Ok. Just figured it out. Looked at too much code to catch my mistake. Here's the solution.
dialog.setContentView(R.layout.auto_layout);
AutoCompleteTextView auto_tv = (AutoCompleteTextView)dialog.findViewById(R.id.role_ac);
I needed to specify to use the Dialog's layout and not the main application's layout that was implied by not using dialog.findViewByID.

Related

Android: Why subtitle in ListView not grey?

I use latest Android Studio and SDK. In preview & real device i see this:
My code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myappname.view.AboutActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewAbout" />
</RelativeLayout>
How i make subtitle text color is gray? Like this:
I'm going out on a limb and assume that you're using the row layout simple_list_item_2.xml (based on the screenshot) which gives you two rows. The problem, if you may call it that, is that depending on the SDK version, the styling for this layout has changed.
On SDK 23, it looks like this:
However, on say SDK 19, it looks like this:
Why?
To understand this we first need to take a look at the xml that generates the rows from simple_list_item_2.xml, you'll see it's a pretty simple layout that uses the now deprecated view TwoLineListItem but that's just a plus on why to use your custom layout.
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingStart"
android:paddingEnd="?attr/listPreferredItemPaddingEnd">
<TextView android:id="#id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#id/text1"
android:textAppearance="?attr/textAppearanceListItemSecondary" />
</TwoLineListItem>
The reason is because of the way the style textAppearanceListItemSecondary is resolved in each SDK version. The style is what gives the text the size, the color, etc. The evolution of the interface in Android has given birth to a huge ecosystem of themes and relying on the default styling will result in inconsistencies like the one you stumbled upon.
What to do about it?
You should use your own layout for this to allow for uniform styling across versions. To do so, please refer to any of the multiple questions covering this matter. But in short it just means creating a layout file, call it for example custom_row.xml and having the layout look exactly as you please. This also gives you total control over placement of the items, extra Views that you may need, and overhead in terms of coding is minimal compared to the SimpleAdapter or ArrayAdapter that perhaps you were using.
Note
You should consider moving your code towards RecyclerView instead of ListView if you haven't already.
You can set Textview property
android:textColor="#color/grey"
in you Adapter layout to change colour of your sub item
Hope this will help

Trying to put several textviews in a Button

I think I might be doing something stupid but I cannot find the answer. Obviously ht ebelow thing doesn't work but how would I do it?
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+#id/date"
android:text="+#id/heading"
android:text="+#id/subheading"
android:drawableLeft="+#id/featuredimage"
/>
I'm basically making an articlebutton to represent an article for a blog. I tried putting textviews inside but it miscounted the ending tags and threw an error.
What is that best way to create one of these buttons?
Dont use Button.
Use a Linear/Relative layout, with the attribute:
android:clickable="true"
Inside this layout add all your TextViews or any other thing you want.

Spinner looks darker than usual

I'm using Spinner in my android app, but I've a problem. It works fine, but its color is darker than they use to have, but I don't know why. Is not a problem with themes or styles, is more, I used Spinners recently for other project and It works perfectly. Did it happen to you? How did you solve it?
I don't use any custom component for this example, so I don't attach the code, but If you need it just ask for.
it may be because you passed different resource id to spinner arrayAdapter.. try passing android.R.layout.simple_spinner_item to your adapter
yourSpinnerArrayAdapter =new ArrayAdapter(this,android.R.layout.simple_spinner_item, your_list);
hope this helps...:)
You can create a custom layout for spinner, it will be more customizable
custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="left"
android:textColor="#000000"
android:padding="10dip"
/>
And your spinner adapter
yourSpinnerArrayAdapter = new ArrayAdapter(this,R.layout.custom_spinner, your_list);

Re-creating Google+ app's UI

I'm fairly new to Android development and planning to build a simple news reader app to further familiarize myself to the platform.
The problem is; I want each of the news-item to look like Google+'s post item below:
Well, basically a big headline picture with a title and some description underneath.
I tried to re-create it using a Button (because the whole thing needs to be clickable by the user) while setting a drawable at the top of the text. It worked quite nicely.. until I decided to add more drawables to it:
Here's my XML code (although I doubt anyone would need it):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_launcher"
android:drawablePadding="8dp"
android:drawableTop="#drawable/temp_jakarta"
android:text="Button" />
</RelativeLayout>
Can you tell me why is this happening or maybe, suggest me another way to accomplish this?
Thanks in advance!
p.s., I'm pretty sorry about the link; as a new user, SO didn't allow me to post images just yet. :(
Put everything inside a RelativeLayout and place one relative to others (event on top, this doesn't matter at all) using the layout_below/above/left_of/right_of/... etc.
See http://developer.android.com/guide/topics/ui/layout/relative.html for usage, http://developer.android.com/reference/android/widget/RelativeLayout.html for a complete reference of the RelativeLayout class and "Android Layout Tricks" series in the Developer Blog for some tips_
http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-1.html
http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-2-reusing-layouts.html
http://android-developers.blogspot.com.es/2009/03/android-layout-tricks-3-optimize-with.html

How do I define a ListView with contents in the layout XML?

I'm prototyping an Android application, and am trying to define the UI before writing any Java code. The basic XML layout stuff works fine, but I can't figure out how to add a ListView with contents. Adding the ListView inside a LinearLayout works fine, but I can't add anything (not even another LinearLayout) as the contents of this ListView.
I tried the fairly obvious way of doing this:
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="Line One"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Line Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:text="Line Three"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ListView>
But it failed (at runtime) with an exception when trying to start the activity:
java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView.
The SDK documentation for the XML layout language is fairly poor, and I can't seem to find any obvious way to make this work. What am I missing?
Bonus question: is there a way to detect problems in the XML layout at build-time, instead of waiting for Android to throw an exception at runtime?
The basic XML layout stuff works fine, but I can't figure out how to add a ListView with contents.
You can't. Your best options are to either use the ListView element with the attribute android:entries pointing to a an array resource(this will show a simple ListView of Strings) or simulate the ListView by replacing it with a ScrollView and then add the content you want in the ListView as the content of the ListView(but it should be something that simulates the rows and not separate content).
Bonus question: is there a way to detect problems in the XML layout at
build-time, instead of waiting for Android to throw an exception at
runtime?
I guess android already does that. Placing content in a ListView is ok in the xml layout(probably) because the ListView is a ViewGroup, but later when android actually tries to add the content to the ListView it will fail, as the exception says, because the operation is unsupported.
you don't really add other views "into" the list view because it's not a type of layout where you can add nested layouts and views.
refer to this tutorial as it shows how to properly populate and manipulate your listview
http://developer.android.com/resources/tutorials/views/hello-listview.html

Categories

Resources