我正在尝试使用自定义适配器和自定义xml文件创建listViw,但是当我在设备上启动应用程序时,它会崩溃。
我只想在我的列表中显示一些文章:
嗯,这是我的文章课:
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;
}
}
这是我的主要活动xml文件( activity_main.xml ):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewArticle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
这是我的文章的自定义XML文件( article_items.xml ):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="0dip"
android:orientation="vertical"
android:layout_weight="1"
android:padding="10dp" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/titreArticle"
style="@style/titreArticleStyle" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/contenuArticle"
android:layout_marginLeft="10dp"
style="@style/contenuArticleStyle" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dateArticle"
style="@style/dateArticleStyle" />
</LinearLayout>
这是我的自定义适配器:
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<Article>{
private Activity activity;
private ArrayList<Article> article;
private static LayoutInflater inflater = null;
public ArticleAdapter(Activity activity, int textViewResourceId, ArrayList<Article> 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;
}
}
最后,我的主要活动课程:
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<Article> 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<Article>();
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;
}
}
我是Android上的新用户,感谢您的帮助和解释! ;)
编辑: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 <unknown>
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