Getting FATAL EXEPTION:main error - android

Below is my code. When i try to run the app it gives me an error. it says there is a fatal exception in main. Can anybody help me out?
This is my main.java
package com.go.nextgendevelopment.uefaeuro2016;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.List;
public class main extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Adapter
setListAdapter(new Myadapter(this,android.R.layout.simple_list_item_1, R.id.textView, getResources().getStringArray(R.array.teams)));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class Myadapter extends ArrayAdapter<String> {
public Myadapter(Context context, int resource, int textViewResourceID, String[] objects) {
super(context,resource,textViewResourceID,objects);
}
#Override
public View getView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.teams);
ImageView iv = (ImageView) row.findViewById(R.id.imageView);
TextView tv = (TextView) row.findViewById(R.id.textView);
tv.setText(items[position]);
if (items[position].equals("ajax")){
iv.setImageResource(R.drawable.logo_ajax);
}
else if (items[position].equals("feyenoord")){
iv.setImageResource(R.drawable.logo_feyenoord);
}
return super.getView(position,convertView, parent);
}
}
}
activity_main.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_gravity="center_horizontal" />
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Thanks in advance!

Remove the setContentview in onCreate()
While setting the list adapter pass your listitem layout

Related

list of items only filling first one

I created a custom adapter to fill the item i have depending on how many elements there is but it only fills the first item and i can't figure out why
main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:id="#+id/customList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/item_text_2"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 2"
android:layout_gravity="right"
android:layout_below="#+id/item_text_1"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/item_text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/item_text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="Default Text 3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />
Custom Class
public class CustomClass {
private String txt1, txt2, txt3;
public CustomClass(String txt1, String txt2, String txt3) {
this.txt1 = txt1;
this.txt2 = txt2;
this.txt3 = txt3;
}
public String getTxt1() {
return txt1;
}
public String getTxt2() {
return txt2;
}
public String getTxt3() {
return txt3;
}
}
MainActivity
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<CustomClass> lst_stuff = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateCustomClass();
populateListView();
}
private void populateCustomClass()
{
lst_stuff.add(new CustomClass("First", "I Got", "FIRST!!!!!"));
lst_stuff.add(new CustomClass("Second", "How did you...", "you know what GG"));
lst_stuff.add(new CustomClass("Third", "I Got", "Last?!! ... I am just too good to be first"));
}
private void populateListView()
{
ArrayAdapter<CustomClass> adapter = new CustomAdapter();
ListView lst = (ListView) findViewById(R.id.customList);
lst.setAdapter(adapter);
}
private class CustomAdapter extends ArrayAdapter<CustomClass>
{
public CustomAdapter()
{
super(MainActivity.this, R.layout.item, lst_stuff);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = convertView;
if (itemView == null)
itemView = getLayoutInflater().inflate(R.layout.item, parent, false);
try
{
CustomClass customClass = lst_stuff.get(position);
TextView textView_1 = (TextView) findViewById(R.id.item_text_1);
textView_1.setText(customClass.getTxt1());
TextView textView_2 = (TextView) findViewById(R.id.item_text_2);
textView_2.setText(customClass.getTxt2());
TextView textView_3 = (TextView) findViewById(R.id.item_text_3);
textView_3.setText(customClass.getTxt3());
}
catch (NullPointerException exception)
{
Toast.makeText(MainActivity.this, "Exception", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}
return itemView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
is there something missing or something doing wrong?
All seems to be ok, but I would change what is on the method getView() by this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item, parent, false);
CustomClass customClass = lst_stuff.get(position);
((TextView) rowView.findViewById(R.id.item_text_1)).
setText(customClass.getTxt1());
((TextView) rowView.findViewById(R.id.item_text_2)).
setText(customClass.getTxt2());
((TextView) rowView.findViewById(R.id.item_text_3)).
setText(customClass.getTxt3());
return rowView;
}
I think that you must obtain first a instance of inflater, and then inflate your view (rowView) with your layout (R.layout.item).
Good luck!

Android ListView working on emulator, but not on device

I am using ListView with a custom ArrayAdapter, and everything is working fine when I run my app in AVD emulator, but when I install it on my phone my app crashes when I touch any ListView item.
Activity xml
<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"
android:background="#drawable/login_background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.frequentbuyer.ui.activity.ShoppingListListActivity" >
<ListView
android:id="#+id/shoppingListsListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addShoppingListButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:cacheColorHint="#00000000" >
</ListView>
<Button
android:id="#+id/addShoppingListButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/shoppingListsListView"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/shoppingListsListView"
android:text="#string/add_shopping_list" />
</RelativeLayout>
List item xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_item_gradient" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:background="#drawable/border_rectangle_shape"
android:orientation="horizontal" >
<TextView
android:id="#+id/shoppingListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/shoppingListItemsNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</RelativeLayout>
Activity class
package com.frequentbuyer.ui.activity;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import roboguice.inject.ContentView;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import com.frequentbuyer.R;
import com.frequentbuyer.exception.DataAccessException;
import com.frequentbuyer.model.ShoppingList;
import com.frequentbuyer.model.User;
import com.frequentbuyer.service.contract.IShoppingListService;
import com.frequentbuyer.ui.adapters.ShoppingListListViewAdapter;
import com.google.inject.Inject;
#ContentView(R.layout.activity_shoppinglist_list)
public class ShoppingListListActivity extends AbstractBaseActivity {
/* shopping list list activity UI components */
#InjectView(R.id.shoppingListsListView)
private ListView shoppingListsListView;
#InjectView(R.id.addShoppingListButton)
private Button addShoppingListButton;
/* shopping list list activity injected services */
#Inject
private IShoppingListService shoppingListService;
/* shopping list list activity received extras */
// received from login activity
#InjectExtra(ActivityParameterNames.AUTHENTICATED_USER)
User loggedInUser;
private List<ShoppingList> allShoppingLists;
/* activity events */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeShoppingListView();
// add listeners
addListeners();
registerForContextMenu(shoppingListsListView);
}
#Override
public void onBackPressed() {
showExitDialog();
}
#Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
if (view.getId() == R.id.shoppingListsListView) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(allShoppingLists.get(info.position).getName());
menu.add(Menu.NONE, 0, 0, R.string.delete_shopping_list);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
try {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
long shoppingListToDeleteId = info.id;
shoppingListService.deleteShoppingList(this, shoppingListToDeleteId);
reloadCurrentActivity();
} catch (DataAccessException e) {
showErrorMessageDialog(e.getLocalizedMessage());
}
return true;
}
/* activity methods */
private void initializeShoppingListView() {
try {
allShoppingLists = shoppingListService.getAllShoppingListsForUser(ShoppingListListActivity.this, loggedInUser.getId());
// pass context and data to the custom shopping list list adapter
ShoppingListListViewAdapter shoppingListListViewAdapter = new ShoppingListListViewAdapter(this, allShoppingLists);
// set shopping list list adapter
shoppingListsListView.setAdapter(shoppingListListViewAdapter);
} catch (DataAccessException e) {
showErrorMessageDialog(e.toString());
}
}
private void addListeners() {
shoppingListsListView.setOnItemClickListener(shoppingListListItemOnClickListener);
addShoppingListButton.setOnClickListener(addShoppingListButtonOnClickListener);
}
/* activity listeners */
private OnItemClickListener shoppingListListItemOnClickListener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long shoppingListId) {
try {
HashMap<String, Serializable> activityParametersMap = new HashMap<String, Serializable>();
activityParametersMap.put(ActivityParameterNames.SELECTED_SHOPPING_LIST, shoppingListService.getShoppingListById(ShoppingListListActivity.this, shoppingListId));
activityParametersMap.put(ActivityParameterNames.AUTHENTICATED_USER, loggedInUser);
startAnotherActivity(ShoppingListListActivity.this, EditShoppingListActivity.class, activityParametersMap);
} catch (DataAccessException e) {
showErrorMessageDialog(e.getLocalizedMessage());
}
}
};
View.OnClickListener addShoppingListButtonOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
HashMap<String, Serializable> shoppingListListActivityParametersMap = new HashMap<String, Serializable>();
shoppingListListActivityParametersMap.put(ActivityParameterNames.AUTHENTICATED_USER, loggedInUser);
ShoppingListListActivity.startAnotherActivity(ShoppingListListActivity.this, CreateShoppingListActivity.class, shoppingListListActivityParametersMap);
}
};
}
Custom adapter class
package com.frequentbuyer.ui.adapters;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.frequentbuyer.R;
import com.frequentbuyer.model.ShoppingList;
public class ShoppingListListViewAdapter extends ArrayAdapter<ShoppingList> {
private final Context context;
private final List<ShoppingList> shoppingLists;
public ShoppingListListViewAdapter(Context context, List<ShoppingList> shoppingLists) {
super(context, R.layout.shoppinglist_list_item, shoppingLists);
this.context = context;
this.shoppingLists = shoppingLists;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// create inflater
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// get rowView from inflater
View rowView = inflater.inflate(R.layout.shoppinglist_list_item, parent, false);
// get the two text view from the rowView
TextView shoppingListNameView = (TextView) rowView.findViewById(R.id.shoppingListName);
// set the text for textView
shoppingListNameView.setText(shoppingLists.get(position).getName());
// get the two text view from the rowView
TextView shoppingListItemNumberView = (TextView) rowView.findViewById(R.id.shoppingListItemsNumber);
// set the text for textView
int shoppingListSize = shoppingLists.get(position).getShoppingItems().size();
shoppingListItemNumberView.setText("(" + shoppingListSize + " item" + (shoppingListSize == 1 ? ")" : "s)"));
// return rowView
return rowView;
}
#Override
public long getItemId(int position) {
return shoppingLists.get(position).getId();
}
}
In some previous version of the app I used ListView in the same way on my phone without any problems, and now it is causing my app to crash.
Any idea why this is happening?
I used USB debugging, and found NullPointerException in logcat. It was thrown in getItemId method of my custom adapter, because my entities had null value for id field in database, for some reason. I defined id fields by using ORMLite with:
#DatabaseField(generatedId = true)
private Long id;
I changed type of id fields from Long to int and reinstalled app on phone and it worked, id fields were populated.

How can i put images in a listView?

I have a ListView but I can´t put images on it.
The error is in Activity_listView at holder.image.setImageDrawable(datos[position].getImage());
The error message is
The method setImageDrawable(Drawable) in the type ImageView is not applicable
for the arguments (ImageView)
Any idea for solve this?
Activity (Activity_listView):
package com.simarro.asteroids;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class Activity_listView extends ActionBarActivity {
private ListView lstOpciones;
ImageView asteroid = (ImageView)findViewById(R.id.img_asteroid);
private puntuacion[] datos = new puntuacion[] {
new puntuacion(asteroid,"Pepito Dominguez", "15489"),
new puntuacion(asteroid,"Pedro Martínez", "16598"),
new puntuacion(asteroid,"Paco Perez", "16332"),
new puntuacion(asteroid,"Rosana Fernandez", "18792"),
new puntuacion(asteroid,"Paco Jones", "960") };
class AdaptadorTitulares extends ArrayAdapter<puntuacion> {
Activity context;
AdaptadorTitulares(Activity context) {
super(context, R.layout.puntuacion, datos);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ViewHolder holder;
if (item == null) {
LayoutInflater inflater = context.getLayoutInflater();
item = inflater.inflate(R.layout.puntuacion, null);
holder = new ViewHolder();
holder.image=(ImageView)item.findViewById(R.id.img_asteroid);
holder.jugador = (TextView) item.findViewById(R.id.LblJugador);
holder.puntuacion = (TextView) item.findViewById(R.id.LblPuntuacion);
item.setTag(holder);
} else {
holder = (ViewHolder) item.getTag();
}
holder.image.setImageDrawable(datos[position].getImage());
holder.jugador.setText(datos[position].getJugador());
holder.puntuacion.setText(datos[position].getPuntuacion());
return (item);
}
}
static class ViewHolder {
ImageView image;
TextView jugador;
TextView puntuacion;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
AdaptadorTitulares adaptador = new AdaptadorTitulares(this);
lstOpciones = (ListView) findViewById(R.id.LstOpciones);
//¡FUUUUUUUUU-SIÓN!
lstOpciones.setAdapter(adaptador);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_list_view, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The xml of content for layout with listView (puntuacion.xml):
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal" >
<ImageView
android:id="#+id/img_asteroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/asteroid" />
<TextView
android:id="#+id/LblJugador"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0"
android:textSize="30px"
android:textStyle="bold" />
<TextView
android:id="#+id/LblPuntuacion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px"
android:textStyle="normal" />
</GridLayout>
The layout where is the listView (listView.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/LstOpciones"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The class for get values (Puntuacion)
package com.simarro.asteroids;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
public class puntuacion
{
private ImageView image;
private String jugador;
private String puntuacion;
public puntuacion(ImageView img, String tit, String sub){
image=img;
puntuacion = sub;
jugador = tit;
}
public ImageView getImage(){
return image;
}
public String getJugador(){
return jugador;
}
public String getPuntuacion(){
return puntuacion;
}
}
Instead of returning ImageView in getImage(), return a Drawable and change it to
public Drawable getImage() {
return image.getDrawable();
}
getDrawable() Docs
As the error says, the method setImageDrawable() takes a Drawable type but you are passing it an ImageView

Unable to get custom spinner in android

I have followed this link to learn about implementing custom spinner. My requirement is just to get drop down menu on clicking a image, it should be like a drop down menu where in i load contents to the list from backend.
But i thought that sample example posted in the above link would work. but its not giving any list on clicking a image icon.
Here is the full source code.
package com.example.testdroid;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
public class MainActivity extends SherlockActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createCustomActionBar();
}
// #Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_main, menu);
// return true;
// }
private void createCustomActionBar()
{
List<String> links = new ArrayList<String>();
links.add("Abc");
links.add("Def");
links.add("Ghi");
LinksAdapter linkAdapter = new LinksAdapter(this, R.layout.externallink, links);
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
IcsSpinner spinner = (IcsSpinner)customNav.findViewById(R.id.spinner);
spinner.setAdapter(linkAdapter);
ImageView refresh = (ImageView) customNav.findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
ImageView settings = (ImageView) customNav.findViewById(R.id.settings);
settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
getSupportActionBar().setCustomView(customNav, new ActionBar.LayoutParams(Gravity.RIGHT));
getSupportActionBar().setDisplayShowCustomEnabled(true);
}
private static class LinksAdapter extends ArrayAdapter<String> {
private List<String> strings;
private Context context;
private LinksAdapter(Context mainActivity, int textViewResourceId, List<String> objects) {
super(mainActivity, textViewResourceId, objects);
this.strings = objects;
this.context = mainActivity;
}
#Override
public int getCount() {
if (strings == null) return 0;
return strings.size();
}
#Override
public String getItem(int position) {
return super.getItem(position);
}
// return views of drop down items
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final String siteLink = strings.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// at 0 position show only icon
TextView site = (TextView) inflater.inflate(R.layout.externallink, null);
site.setText(siteLink);
site.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(siteLink.getUrl()));
//context.startActivity(i);
}
});
return site;
}
// return header view of drop down
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.custom_show_action_bar, null);
}
}
}
Here is my XML file, i.e layout xml file.
custom_show_action_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
>
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="20dp"
android:layout_gravity="center"
/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:paddingLeft="10dp"
android:layout_gravity="center"
android:background="#ffffff"
android:id="#+id/refresh"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher"
android:paddingRight="20dp"
android:background="#ffffff"
android:layout_gravity="center"
android:id="#+id/settings"/>
</LinearLayout>
External_links.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Guide me where i'm wrong.

Resource Not found exception in android

i am trying to create a listview in my android application. But i am getting Resource Not found Exception while running the project.
activity_main.xml :
<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=".MainActivity" >
<include
android:id="#+id/title_bar"
layout="#layout/title_bar" />
<ListView
android:id="#+id/FeedList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title_bar"
></ListView>
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<ImageButton
android:id="#+id/FeedType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#E0E0E0"
android:padding="9dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/FeedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feed Title"
android:textSize="18sp"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/FeedPostedBy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedTitle"
android:text="by FeedOwner"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:textSize="10sp"
/>
<TextView
android:id="#+id/FeedContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FeedPostedBy"
android:layout_toRightOf="#+id/FeedType"
android:paddingLeft="5dp"
android:paddingTop="8dp"
android:text="The content posted as a feed by the feed owner will appear here"
android:textSize="10sp"/>
</RelativeLayout>
ListAdapter Class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
public class ListAdapter extends BaseAdapter{
private Activity listAdapterActivity = null;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater;
public ListAdapter(Activity ListActivity,ArrayList<HashMap<String, String>> listData)
{
listAdapterActivity = ListActivity;
data = listData;
inflater = (LayoutInflater)listAdapterActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItem = convertView;
if(listItem == null)
listItem = inflater.inflate(R.id.FeedList, null);
ImageButton feedIcon = (ImageButton) listItem.findViewById(R.id.FeedType);
TextView feedTitle = (TextView) listItem.findViewById(R.id.FeedTitle);
TextView feedOwner = (TextView) listItem.findViewById(R.id.FeedPostedBy);
TextView feedContent = (TextView) listItem.findViewById(R.id.FeedContent);
HashMap<String, String> feedData = new HashMap<String, String>();
feedData = data.get(position);
feedIcon.setImageResource(R.drawable.ic_launcher);
feedTitle.setText(feedData.get("FeedTitle"));
feedOwner.setText(feedData.get("FeedOwner"));
feedContent.setText(feedData.get("FeedContent"));
return listItem;
}
}
MainActivity class :
package com.tcs.internal.prime;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> feedList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> data = new HashMap<String, String>();
data.put("FeedTitle","Application crashed when launched in Windows 7");
data.put("FeedOwner", "By Venkatramanan");
data.put("FeedContent","Launch the financial aid adminstration in windows 7 environment");
feedList.add(data);
data.clear();
data.put("FeedTitle","Application crashed when launched in Windows 8 ");
data.put("FeedOwner", "By Siva Guru");
data.put("FeedContent","Launch the financial aid adminstration in windows 8 environment");
feedList.add(data);
ListView feedListView = (ListView)findViewById(R.id.FeedList);
ListAdapter listAdapter = new ListAdapter(this, feedList);
feedListView.setAdapter(listAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I am getting this following exception :
android.content.res.Resources$NotFoundException: Resource ID #0x7f070001 type #0x12 is not valid
All you have to inflate the custom layout in your listview's adapter's getview method as
listItem = inflater.inflate(R.layout.list_item, null);
instead of
listItem = inflater.inflate(R.id.FeedList, null);

Categories

Resources