ListView with Custom Adapter plus Custom XML-Collection of common programming errors
I’m trying to make a listViw using a custom adapter plus custom xml file, but when I launch my app on my device, it crash.
I just want ton display in my listView some articles:
- Title (TextView)
- Content (TextView)
- Date (TextView) I will change to Date after…
Well, here is my Article class:
package com.example.articles;
public class Article {
private String titreArticle;
private String contenuArticle;
//private Date dateArticle;
private String dateArticle;
public Article(String titre, String contenu, String date){
titreArticle = titre;
contenuArticle = contenu;
dateArticle = date;
}
public String getTitreArticle() {
return titreArticle;
}
public void setTitreArticle(String titreArticle) {
this.titreArticle = titreArticle;
}
public String getContenuArticle() {
return contenuArticle;
}
public void setContenuArticle(String contenuArticle) {
this.contenuArticle = contenuArticle;
}
public String getDateArticle() {
return dateArticle;
}
public void setDateArticle(String dateArticle) {
this.dateArticle = dateArticle;
}
public String toString(){
return this.titreArticle +","+ this.contenuArticle +","+ this.dateArticle;
}
}
Here is my main activity xml file (activity_main.xml):
Here is my custom XML file for my articles (article_items.xml):
Here is my custom Adapter:
package com.example.articles;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ArticleAdapter extends ArrayAdapter{
private Activity activity;
private ArrayList article;
private static LayoutInflater inflater = null;
public ArticleAdapter(Activity activity, int textViewResourceId, ArrayList article){
super(activity,textViewResourceId,article);
try {
this.article = article;
this.activity = activity;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} catch (Exception e) {
System.out.println(e);
}
}
@Override
public int getCount() {
return article.size();
}
public Article getItem(Article position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView titreArticle;
public TextView contenuArticle;
public TextView dateArticle;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
try {
if (convertView == null) {
vi = inflater.inflate(R.layout.article_items, null);
holder = new ViewHolder();
holder.titreArticle = (TextView) vi.findViewById(R.id.titreArticle);
holder.contenuArticle = (TextView) vi.findViewById(R.id.contenuArticle);
holder.dateArticle = (TextView) vi.findViewById(R.id.dateArticle);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.titreArticle.setText(article.get(position).getTitreArticle());
holder.contenuArticle.setText(article.get(position).getContenuArticle());
holder.dateArticle.setText(article.get(position).getDateArticle());
} catch (Exception e) {
System.out.println(e);
}
return vi;
}
}
Finaly, my main activity class:
package com.example.articles;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listeArticle = null;
private ArticleAdapter adapter = null;
private ArrayList articleArrayList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listeArticle = (ListView) findViewById(R.id.listViewArticle);
articleArrayList = new ArrayList();
articleArrayList.add(new Article("Title1","Content1","11-09-2013"));
adapter = new ArticleAdapter(MainActivity.this,0,articleArrayList);
listeArticle.setAdapter(adapter);
}
@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;
}
}
I’m a new user on Android, thanks for your help and explanations guys! 😉
EDIT: Logcat
09-11 14:44:49.414: D/ddm-heap(223): Got feature list request
09-11 14:44:50.324: I/System.out(223): android.view.InflateException: Binary XML file line #15: Error inflating class
09-11 14:44:50.365: D/AndroidRuntime(223): Shutting down VM
09-11 14:44:50.374: W/dalvikvm(223): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
09-11 14:44:50.374: E/AndroidRuntime(223): Uncaught handler: thread main exiting due to uncaught exception
09-11 14:44:50.434: E/AndroidRuntime(223): java.lang.NullPointerException
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.AbsListView.obtainView(AbsListView.java:1276)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.ListView.measureHeightOfChildren(ListView.java:1147)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.ListView.onMeasure(ListView.java:1060)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.View.measure(View.java:7964)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.View.measure(View.java:7964)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.View.measure(View.java:7964)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.View.measure(View.java:7964)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.View.measure(View.java:7964)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.os.Looper.loop(Looper.java:123)
09-11 14:44:50.434: E/AndroidRuntime(223): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-11 14:44:50.434: E/AndroidRuntime(223): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 14:44:50.434: E/AndroidRuntime(223): at java.lang.reflect.Method.invoke(Method.java:521)
09-11 14:44:50.434: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-11 14:44:50.434: E/AndroidRuntime(223): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-11 14:44:50.434: E/AndroidRuntime(223): at dalvik.system.NativeStart.main(Native Method)
09-11 14:44:50.495: I/dalvikvm(223): threadid=7: reacting to signal 3
09-11 14:44:50.657: I/dalvikvm(223): Wrote stack trace to '/data/anr/traces.txt'
09-11 14:45:11.764: I/Process(223): Sending signal. PID: 223 SIG: 9