listView.setAdapter() is crashing my program - android

listView.setAdapter() is crashing my program. Anyone have any idea why the crash might be happening?
Logcat error:
AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
Activity:
public class SavedVideosFragment extends Fragment {
View rootview;
MySQLiteHelper mydb;
private SavedVideoAdapter mSavedVideoAdapter;
ListView listView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.saved_video_fragment_layout,container,false);
getSavedVideo();
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
return rootview;
}
public void getSavedVideo() {
mydb = new MySQLiteHelper(getActivity());
ArrayList savedVideoIDs = mydb.getAllSavedVideo();
mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
listView.setAdapter(mSavedVideoAdapter);
}
}
Adapter:
public class SavedVideoAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Object> mData = new ArrayList<Object>();
MySQLiteHelper mydb;
private LayoutInflater mInflater;
public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
Object obj = mData.get(position);
return "";
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Cursor res = res = mydb.getData((Integer)mData.get(position));
convertView = mInflater.inflate(R.layout.saved_video_adapter, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView activity = (TextView) convertView.findViewById(R.id.activity);
TextView activityDate = (TextView) convertView.findViewById(R.id.activityDate);
ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
activity.setText(res.getColumnIndex("title"));
return convertView;
}
}
ListView 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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/savedVideoListView"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Adapter Layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/thumbnail">
<TextView
android:id="#+id/activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="#+id/activityDate"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="#+id/activity"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="fill_parent"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/x"
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="#drawable/x" />
</RelativeLayout>
</RelativeLayout>

AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
Because listView is not initialized.
Call getSavedVideo() method after initializing listView :
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
getSavedVideo();

Related

Custom adapter in fragment listview

I am trying to add a custom adapter to a listview within a fragment. It's running fine but it is not displaying anything in fragment. I've tried many methods but I am not able to get it to work. Any help would be great. Thanks in advance. Here is my code:
Fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/UniFeeList"
android:layout_centerHorizontal="true"
android:clickable="true"
>
</ListView>
<android.support.design.widget.FloatingActionButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_input_add"
android:id="#+id/addQuestion"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dip"
/>
</RelativeLayout>
Fragment.Java
public class UniFeedFragment extends Fragment {
ListView uniFeedListview;
FloatingActionButton askQuestion;
String stdEmail, UniEmail;
TextView tv;
Post post;
String date,time;
ListView UniFeedList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.unifeedfragment, container, false);
//Getting Time
DateFormat df = new SimpleDateFormat("HH:mm");
time = df.format(Calendar.getInstance().getTime());
//Getting Date
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
date = dateFormatter.format(today);
//Getting data from news feed search bar
UniFeed uniFeed = (UniFeed) getActivity();
stdEmail = uniFeed.stdEmail;
UniEmail = uniFeed.uni.getEmail();
//Setting Listview
UniFeedList = (ListView) view.findViewById(R.id.UniFeeList);
final ArrayList<Post> postArray = new ArrayList<Post>();
PostListAdapter PostList = new PostListAdapter(this.getActivity(), R.layout.activity_uni_feed ,postArray);
UniFeedList.setAdapter(PostList);
return view;
}
PostListAdapter.java
public class PostListAdapter extends BaseAdapter {
Context context = null;
ArrayList<Post> postData = new ArrayList<>();
LayoutInflater inflater;
public PostListAdapter(Context context, int activity_uni_feed, ArrayList<Post> postData){
this.context = context;
this.postData = postData;
}
#Override
public int getCount() {
return postData.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.unifeedpost, parent, false);
return convertView;
} }
unifeedPost.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stdProfileImage"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Name"
android:id="#+id/stdName"
android:layout_alignTop="#id/stdProfileImage"
android:layout_toRightOf="#+id/stdProfileImage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Date and Time"
android:id="#+id/time"
android:layout_below="#+id/stdName"
android:layout_toRightOf="#+id/stdProfileImage"
android:layout_toEndOf="#+id/stdProfileImage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postContent"
android:layout_below="#id/time"
android:text="Test Post"
android:layout_marginTop="10dip"
android:layout_toRightOf="#id/stdProfileImage"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/postContent"
android:layout_toRightOf="#id/stdProfileImage"
android:layout_marginTop="10dip"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:layout_weight="0.5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment"
android:layout_weight="0.5"
/>
</LinearLayout>

Unconditional layout inflation from view adapter

I'm getting a warning like Unconditional layout inflation which is causing the layout to exit and app goes crashing.I need to inflate this layout without crashing.
the crashing is on the following code.
I have the code for the custom adapter set.
public class CustomAdapter extends BaseAdapter {
private List<Response.NodesEntity> mMovieitem;
private Context mContext;
private LayoutInflater inflater;
public CustomAdapter(Context mContext, List<Response.NodesEntity> mMovieitem) {
this.mContext = mContext;
this.mMovieitem = mMovieitem;
}
#Override
public int getCount() {
return mMovieitem.size();
}
#Override
public Object getItem(int position) {
return mMovieitem.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.s, parent, false);
Response.NodesEntity item = (Response.NodesEntity) getItem(position);
ImageView thumbnail = (ImageView) rowView.findViewById(R.id.thumbnail);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView rating = (TextView) rowView.findViewById(R.id.video_url);
String imageUrl = item.getVideoTumbnail().getSrc();
Picasso.with(mContext).load(imageUrl).into(thumbnail);
title.setText(item.getTitle());
rating.setText(item.getHlsVideo());
return rowView;
}
}
The Xml is in this format . I'm inflating this layout and
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/video_list"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="info.androidhive.materialdesign.activity.FriendsFragment">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/thumbnail"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_material_light"
android:scaleType="centerCrop" />
<TextView
android:layout_marginBottom="10dp"
android:layout_gravity="bottom"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/thumbnail"
android:layout_toEndOf="#+id/thumbnail"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:typeface="sans"
android:textSize="22sp"
android:textColor="#ffffff" />
<TextView
android:layout_marginTop="20dp"
android:id="#+id/video_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_toEndOf="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:visibility="invisible"/>
</android.support.v7.widget.CardView>
</RelativeLayout>
use View holder method for inflating each rows

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

I can't see ListFragment android?

I create a ListFragment for show Text & Image my code hase not any error but when i run it , it get me crash & when I see Log it don't get me any Message ?
My StartActivity.class :
public class StartActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new Setting_Fragment()).commit();
}
}
My activity_start.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/back"
tools:context="in.project.StartActivity" >
<ImageView
android:id="#+id/imgHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#e42769"
android:src="#drawable/head" />
<LinearLayout
android:id="#+id/LFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="6" >
<ImageView
android:id="#+id/img_Setting_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting_note" />
<ImageView
android:id="#+id/img_Calendar"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_calendar" />
<ImageView
android:id="#+id/img_Mail"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_mail" />
<ImageView
android:id="#+id/img_Review_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_review_note" />
<ImageView
android:id="#+id/img_Help"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_help" />
<ImageView
android:id="#+id/img_Setting"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting" />
</LinearLayout>
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/LFooter"
android:layout_below="#+id/imgHeader" />
</RelativeLayout>
My SettingArrayAdapter.class :
public class SettingArrayAdapter extends ArrayAdapter<Instructure_setting>{
private Context context;
private List<Instructure_setting> objects;
public SettingArrayAdapter(Context context, int resource, List<Instructure_setting> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#SuppressLint({ "ViewHolder", "InflateParams" })
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Instructure_setting _Data = objects.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listview_setting_fragment, null);
ImageView image = (ImageView) view.findViewById(R.id.img_message);
image.setImageResource(_Data.getImageResource());
TextView tv = (TextView) view.findViewById(R.id.txt_message);
tv.setText(_Data.gettxtMessage());
return view;
}
}
My Setting_Fragment.class :
public class Setting_Fragment extends ListFragment{
List<Instructure_setting> DATA = new Setting_Fragment_Data().getMessages();
public Setting_Fragment()
{
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingArrayAdapter adapter = new SettingArrayAdapter(getActivity(),
R.layout.listview_setting_fragment,
DATA);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.setting_fragment, container, false);
return rootView;
}
}
My setting_fragment.xml :
<?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=".StartActivity" >
<ListView
android:id="#+id/lstview_Setting_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
My Setting_Fragment_Data.class :
public class Setting_Fragment_Data {
private List<Instructure_setting> _Messages = new ArrayList<Instructure_setting>();
public List<Instructure_setting> getMessages() {
return _Messages;
}
public Setting_Fragment_Data() {
_Messages.add(new Instructure_setting("AAA", R.drawable.period_color));
_Messages.add(new Instructure_setting("BBB", R.drawable.password_color));
_Messages.add(new Instructure_setting("CCC", R.drawable.cleare_data_color));
_Messages.add(new Instructure_setting("DDD", R.drawable.chanel_connect_color));
_Messages.add(new Instructure_setting("EEE", R.drawable.backup_color));
_Messages.add(new Instructure_setting("FFF", R.drawable.review_note_color));
_Messages.add(new Instructure_setting("GGG", R.drawable.notification_color));
_Messages.add(new Instructure_setting("HHH", R.drawable.sync_server_color));
}
}
My Instructure_setting.class :
public class Instructure_setting {
// constants for field references
public static final String TXT_Message = "TextMessage";
public static final String IMAGE_RESOURCE = "imageResource";
// private fields
private String txtMessage;
private int imageResource;
// getters and setters
public String gettxtMessage() {
return txtMessage;
}
public void settxtMessage(String txtMessage) {
this.txtMessage = txtMessage;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
// Used when creating the data object
public Instructure_setting(String msg, int imageResource) {
this.txtMessage = msg;
this.imageResource = imageResource;
}
// Create from a bundle
public Instructure_setting(Bundle b) {
if (b != null) {
this.txtMessage = b.getString(TXT_Message);
this.imageResource = b.getInt(IMAGE_RESOURCE);
}
}
// Package data for transfer between activities
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString(TXT_Message, this.txtMessage);
b.putInt(IMAGE_RESOURCE, this.imageResource);
return b;
}
// Output txtMessage data
#Override
public String toString() {
return txtMessage;
}
}
This is my listview_setting_fragment.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:paddingBottom="10dp">
<ImageView
android:id="#+id/img_message"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:contentDescription="placeholder" />
<TextView
android:id="#+id/txt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/img_message"
android:text="placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
NOTICE : I am using from appcompat_v7 .
When you use ListFragment or ListActivity then ListView id in xml must be :
android:id="#id/android:list
Change your ListView id in xml :
<ListView
android:id="#id/android:list"
Instead of
<ListView
android:id="#+id/lstview_Setting_fragment"
Note : Try to set List Adapter in onCreateView() instead of onCreate() in Fragment.

java.lang.NullPointerException with ListView and BaseAdapter

I tried to use a ListView with a BaseAdapter. I had a java.lang.NullPointerException on this line:
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
Where I'm doing wrong?
This is my main activity:
public class ListaUtenti extends Activity {
private ListView listView;
private ListAdapter adapter;
private List<Persona> persona = new LinkedList<Persona>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_utenti);
listView = (ListView)findViewById(R.id.listaUtenti);
adapter = new BaseAdapter() {
#Override
public int getCount() {
return persona.size();
}
#Override
public Persona getItem(int position) {
return persona.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item_persona, null);
}
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
final Persona p = getItem(position);
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
descrizione.setText(getResources().getString(R.string.descrizione, p.cognome));
return view;
}
};
listView.setAdapter(adapter);
}
#Override
protected void onStart(){
super.onStart();
persona.clear();
persona.add(new Persona("nome1","cognome1"));
persona.add(new Persona("nome2","cognome2"));
persona.add(new Persona("nome3","cognome3"));
listView.setAdapter(adapter);
}
}
This is the ListView in the xml file activity_lista_utenti:
<ListView
android:id="#+id/listaUtenti"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:listitem="#android:layout/simple_list_item_1" />
This is the xml file list_item_persona:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="5dp"
android:src="#drawable/ic_contact_picture"
android:contentDescription="#string/immagine_profilo" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:background="#color/yellow2"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:background="#color/yellow"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="15sp" />
</RelativeLayout>
Get textview from view's findViewById,i.e.
Change
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
to
final TextView titolo = (TextView)view.findViewById(R.id.firstLine);
final TextView descrizione = (TextView)view.findViewById(R.id.secondLine);
Since they are part of list_item_persona view.

Categories

Resources