Null exception in android 4.1 but working in 4.2 - java

Well I have this
in xml
<Spinner
android:id="#+id/spinnerLanguages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
and In Fragment code file
I have inflated in oncreate
viewInflate = inflater.inflate(R.layout.fragment_search, null);
and
spinnerLanguages = (Spinner) viewInflate.findViewById(R.id.spinnerLanguages);
String[] items1 = new String[] {"lang1", "lang2", "lang3","lang4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, items1);
spinnerLanguages.setAdapter(adapter);
But i am getting Null exception in this line
spinnerLanguages.setAdapter(adapter);
in jellybean(4.1) but it is working fine in 4.2+
How can i solve this problem?
Thank you.

You are probably giving different version of the layout file fragment_search.xml in your layout-vx directories: check how many fragment_search.xml exist in your layour directories (e.g. inside layout-v17 vs. layout or layout-v16).

Related

ArrayList Adapter (for ListView) only displaying the first added item

Edit3: My own item list layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
/>
Edit2: Honestly, I might just make a for-loop that creates buttons for each file. This is way too much of a headache to be worth the time.
Edit: I'd like to emphasize the fact that I've copy+pasted my exact code into a new test app and it works fine. That might give you guys a clue.
After tons of debugging, I've narrowed down a problem. I'm trying to add items (files) to an ArrayList, then put that into an ArrayAdapter, and finally display the items in a ListView. The problem is that only the first added item is being displayed.
Here's how I was trying to do it:
ListView listView = (ListView) view.findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getContext().getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
for(int i = 0; i < length; i++){
templateList.add(templateFiles[i].getName());
}
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
The templateFiles[] array properly gets the 8 files in Internal Storage (confirmed via logcat/debugger), but only the first item is displayed in the ListView. Here's a clearer look at the issue:
// If I replace the for loop with this:
templateList.add(templateFiles[1].getName());
templateList.add(templateFiles[0].getName());
Only templateFiles[1] is displayed. Similarly, if I do this:
templateList.add(templateFiles[0].getName());
templateList.add(templateFiles[1].getName());
Only templateFiles[0] is displayed. Any ideas on what's going wrong?
Here's my XML:
<LinearLayout
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.template_housing.MyTemplatesFrag"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/my_templates"
android:textSize="34sp"
android:textColor="#color/black"
android:background="#color/Gold1"
android:gravity="center_horizontal"
android:padding="5dp"
android:layout_marginBottom="10dp"
/>
<ListView
android:id="#+id/templateFilesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
If you are updating the content of your adapter you should be calling notifyDataSetChanged() as well to make sure your adapter gets to know that your content did change
I copied your code and ran it in Android Studio. It seems NO problem.
Here is my activity code(same xml code), the only difference is that I use an Activity, you may use a Fragment.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
//case 1: in my project, length is 1, show only one element, see case2.
// for (int i = 0; i < length; i++) {
// templateList.add(templateFiles[i].getName());
// }
//case 2: try to add simple strings,
//because you said put simple strings in the list could cause the problem, too.
templateList.add("a1");
templateList.add("a2");
templateList.add("a3");
ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
}
}
I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.
Try to convert it to an array of Strings:
String [] templateListArray = templateList.toArray(new String[templateList.size()]);
and then do the assignment as follows:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
templateListArray);
Hope this will fix it.
Can you post the simple list item 1 XML file too. This issue is not Java code related it has something to do with XML and size of elements. Try deleting that text view on top of the list view and your simple list item view should have wrap content as layout height.
And if you still want to use the textview you can use weight attribute. Give your textview a weight of 0 and the list view a weight of 1.
It may be possible that your array adapter is not setting properly and therefore it is not working properly, so try to replace your array adapter by below code and check if it is working,
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, templateList);

Set background for items in a spinner using a drawable resource - java android studio

I've created a spinner using the code below. Now I want to use a drawable resource called circle.xml, to set the background for each item in the spinner so that each number appears inside a circle.
Spinner equationSpinner = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("");
spinnerArray.add("1");
spinnerArray.add("2");
spinnerArray.add("3");
spinnerArray.add("4");
spinnerArray.add("5");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
equationSpinner.setAdapter(spinnerArrayAdapter);
I can use the circle as a background in a TextView so I tried creating the spinnerArray as an array of TextViews but that appeared to be incompatible with the simple_spinner_dropdown_item.
Anybody have an idea how I can do this? I just want each number to appear inside a circle in the spinner. I will also need to be able to access the number selected in the spinner.
Thanks
P.S. Is there any way in Android to create a 'slot machine' style spinner like you have in iOS? I prefer the look of those.
EDIT:
I've now found that I can use my circle resource by creating a custom layout (called spinner_item.xml) like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="28dip"
android:layout_height="28dip"
android:gravity="center"
android:textSize="20sp"
android:background="#drawable/circle"
android:textColor="#ff0000" />
I then set the adapter for my spinner programmatically like this:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(adapter);
However, this method requires that I have a predefined string array set up in my strings.xml file (R.array.equations). I need to be able to specify the array of strings to be used programmatically depending on the state of the app. Can I modify the array once its set up?
EDIT 2:
Additionally I've found I can set the background of the dropdown items and use my own programmatical array like this:
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(spinnerArrayAdapter);
The only problem then is being able to apply the resource to the background of the spinner itself (not the drop down items). There does not appear to be an equivalent to setDropDownViewResource for the main spinner item.
lets say textview_background.xml is the background you want to give to the textView to Spinner
make a file in drawable and name it this textview_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
android:width="3dp"
android:color="#android:color/tertiary_text_light" />
</shape>
make a Spinner in your activity
<Spinner
android:id="#+id/SpinnerOneActivity_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
make a layout with name list_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="#drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#android:color/black" />
make a class which will fill data from a list to your spinner, in this code your Adapter class is taking list of String but you can have your own custom object list given to it to populate the spinner, watch out how adapter's constructor parameters.
first-Context
Second-layout which will be given to each item in spinner
Third-a textView id from the above layout
Forth-your list of objects which you want to populate
TestAdapter.java
public class TestAdapter extends ArrayAdapter {
private ArrayList<String> strings;
private Context context;
private LayoutInflater layoutInflater;
public TestAdapter(Context context, int resource,int id, ArrayList<String> strings) {
super(context, resource, id, strings);
this.context = context;
this.strings = strings;
this.layoutInflater = LayoutInflater.from(this.context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = layoutInflater.inflate(R.layout.list_text_view, parent, false);
TextView textView = (TextView) view.findViewById(R.id.text1);
textView.setText("" + strings.get(position));
return view;
}
}
in your activity you can write this code
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("Two");
list.add("Three");
list.add("four");
Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
As you wanted you can increase the list or generate things dynamically to add or remove from the list once your list is attached to adapter which will be provided to Spinner you can call notifyDataSetChanged() on adapter to updated the list
Check this answer about Android Wheel: https://stackoverflow.com/a/9503372/3677394
There's a very good 3rd party open source project called Android Wheel
that pretty much does everything involved in creating a slot machine
for you. And if you want to customise it, it's under an Apache
licence, so no issues there.

android Listview not showing anything

I am working on a first basic android app as a university project and I have an array list of names. I want it to be put into a ListView. The problem I have is nothing appears when I bring in the ListViewunder the xml document. I don't get the basic template for item1, sub item 1 etc.
Then when I set up the array to play in the list view still empty list. Below I have the xml document
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="277dp"
android:id="#+id/listView" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" android:layout_marginTop="7dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
then here is the java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
populateListView();
}
private void populateListView() {
String[] myItems = {"frank","george","alice", "anna"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.main, myItems);
ListView list = (ListView)findViewById(R.id.listView);
list.setAdapter(adapter);
}
Your problem is that you have the same layout R.layout.main for two different use.
You set the content view as setContentView(R.layout.main); where your ListView is but you also use this layout with your adapter as new ArrayAdapter<String>(this, R.layout.main, myItems);.
So with your code, you don't have any item layout to display your array myItems. You need to create another layout as R.layout.item_layout with a TextView inside (or use an android layout like android.R.layout.simple_list_item_1) for your adapter.
Note: to avoid some bugs with ListView, you have also to set the height to match_parent or fill_parent
Try this for your adapter:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myItems);
The adapter has to inflate a TextView. You can make your own TextView (if you want to customize it) in XML.
Then it will be:
ArrayAdapter<String> adapter=
new ArrayAdapter<String>(this, R.layout.yourcustomtextview, myItems);
The default array adapter in Android takes the layout for the row as the second argument.
Change your ArrayAdapter constructor to something like this:
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)

Android spinner in layout

I am trying to populate a spinner but I seem to be missing something in my layout file
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, R.layout.spinner_view,
getResources().getStringArray(R.array.cuisines));
I can't find R.layout.spinner_view and can only assume that I have to make it myself in my layout file. How do I do that?
Include spinner in xml file as:
<Spinner
android:id="#+id/spin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
and in activity:
Spinner spinner=(Spinner) findViewById(R.id.spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item,R.array.cuisines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Use import com.companyname.product.R;
instead of
import android.R;
If you want to load with default spinner view, then use,
ArrayAdapter<String> cuisines = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.cuisines));

Change font size in ListView - Android/Eclipse

How can I change the font size in a ListView element? In my main.xml file, I have tried several different values in for android:textSize (pt,px,sp,dp) and nothing seems to change it.
Here is what I have currently for the in my main.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:textColor="#ffffff"
android:background="#000080"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:dividerHeight="1px"
android:layout_marginTop="5px"
android:textSize="8px"/>
Here is my Java:
package com.SorenWinslow.TriumphHistory;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class TriumphHistory extends ListActivity {
String[] HistoryList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter;
HistoryList = getResources().getStringArray(R.array.history);
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
setListAdapter(adapter);
}
}
Go into layout create a new xml file named mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
now in the code
adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,HistoryList);
change it to
adapter = new ArrayAdapter<String> (this,R.layout.mylist,HistoryList);
2 ways to go:
Copy simple_list_item_1.xml from Android sources, modify it and then use it instead of android.R.layout.simple_list_item_1;
Use BaseAdapter and modify font size in getView(..) call.
I'd suggest you go with latter.
Please create another file named list_item.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="fill_parent"
android:padding="6dp"
android:textSize="21sp">
The quick answer is that you can only change the text size by using your own layout rather than the android.R.layout.simple_list_item one. By doing that you can change the TextView properties such as the text size. However if you haven't worked with lists with your own layouts, they can appear a bit complex to use the first time. I'll try to come back and edit this answer to provide more information later, but I suggest you look for tutorials on custom list views - that will show you how to inflate your own layout files.
Create another xml file for the Text that should be in the list rows..
and just add android:layout_height="?android:attr/listPreferredItemHeight" in your TextView :D (put it in a relative layout rather than a linear layout)
and then use your previous ListView xml for the setContentView(R.layout.listview)
and the other xml file in your ArrayAdapter code
ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.yoursecondLayoutforthelistswithText, R.id.titles, HistoryList)
the R.id.titles is the id for the TextView
you can save font size in sharedprefs and then recreate your adapter
SharedPreferences.Editor ed = mSharedPreferences.edit();
ed.putFloat("fontTwosize", sizeTwo);
ed.commit();
adapterz = new LazyziyaratAdapter(MainActivity.this,
ziyaratList);
listz.setAdapter(adapterz);
and in your getview method of adapter
holder.text.setTextSize(TypedValue.COMPLEX_UNIT_SP,G.mSharedPreferences.getFloat("fontTwosize", 25));

Categories