gpt4 book ai didi

Android,ListView测试代码上的空指针异常

转载 作者:行者123 更新时间:2023-11-30 04:34:59 24 4
gpt4 key购买 nike

我正在构建一个应用程序,它使用 ListView 和一个扩展 BaseAdapter 的自定义适配器来处理 ListView 的数据。代码如下:

newlist.java 编译/运行良好

public class newslist extends Activity {

public static final String tag = "newslist";
ListView listNews;
MyListAdapter listAdapter;


/** Set or Grab the URL */
public static final String parseURL = "http://www.example.com.gr/article.php";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslist);

/** Array Lists */
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> links = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();

Log.d(newslist.tag, "****** parseURL = " + parseURL);

listNews = (ListView) findViewById(R.id.listNews);

try {
/** Open URL with Jsoup */
Document doc = Jsoup.connect(parseURL).get();

/** Grab classes we want */
Elements pcontent = doc.getElementsByClass("content_title");
Elements pdates = doc.getElementsByClass("content_datecreated_left");

/** Loop for grabbing TITLES within parent element */
for (Element ptitles : pcontent) {

/** Grab Anchors */
Elements ptitle = ptitles.getElementsByTag("a");
for (Element title : ptitle) {
titles.add(title.text());
}
}

/** Loop for grabbing LINKS within parent element */
for (Element plinks : pcontent) {
/** Grab anchors */
Elements plink = plinks.getElementsByTag("a");
for (Element link : plink) {
links.add(link.attr("abs:href")); /** parse absolute address */
}
}

/** Loop for grabbing DATES within parent element */
for (Element pdate : pdates) {
dates.add(pdate.text()) ;
}

//TODO: Regex on Date

//String content: Main Activity Content

int i=0;
int num = titles.size();
String[] printDates = new String[num];
for (i=0; i < num; i++)
{
//substring(25) leaves a space after the date, eg "26/6/2011 "
//content[i] = titles.get(i) + "\n Date: " + dates.get(i).substring(25);
printDates[i] = dates.get(i).substring(25);
}

/** Create an ArrayAdapter, that will actually make the Strings above
* appear in the ListView */
listAdapter = new MyListAdapter(this, titles, dates);
listNews.setAdapter(listAdapter);

} catch (Exception e) {
Log.e(newslist.tag, "****** Failed to Parse URL:" + e.getMessage());
e.printStackTrace();
}

} /*- OnCreate End -*/

} /*- Class End -*/

MyListAdapter.java 在第 75 行运行 NPE:

public class MyListAdapter extends BaseAdapter {

public final static String tag = "MyListAdapter";
public Context context;
public ArrayList<String> title;
public ArrayList<String> date;
public LayoutInflater inflater;

public MyListAdapter(Activity context, ArrayList<String> title, ArrayList<String> date) {
super();
this.context = context;
this.title = title;
this.date = date;

this.inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
// Auto-generated method stub
return this.title.size();
}

public Object getItem(int position) {
//Auto-generated method stub
return this.title.get(position);
}

public long getItemId(int position) {
// Auto-generated method stub
return position;
}

private static class ViewHolder {
TextView titleView;
TextView dateView;
}

public View getView(int position, View convertView, ViewGroup parent)
{
// Auto-generated method stub
ViewHolder holder;

Log.d(tag, "****** convertView: " + convertView);

if (convertView == null)
{
convertView = inflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.titleView = (TextView) convertView.findViewById(R.id.listTitle);
holder.dateView = (TextView) convertView.findViewById(R.id.listDate);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Log.d(tag, "****** Title: " + title.get(position));
Log.d(tag, "****** findViewById: " + convertView.findViewById(R.id.listTitle));
Log.d(tag, "****** holder.titleView: " + holder.titleView);


holder.titleView.setText(title.get(position));
holder.dateView.setText(date.get(position));

//notifyDataSetChanged();

return convertView;
}

}

第 75 行是:

holder.titleView.setText(title.get(position));

但是我已经将问题追踪到第 62 行:

holder.titleView = (TextView) convertView.findViewById(R.id.listTitle);

从我的调试消息看来,holder.titleViewnull

我已尝试清理/删除 bin 文件夹并重建项目,但均无济于事。我认为问题在于未找到 View R.id.listTitle。但我不知道为什么。

我还将包含两个用于预览的 xml 文件

新闻列表.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="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
android:gravity="center|top"
android:textSize="20dip"
android:textStyle="bold"
android:text="@string/titleNewslist"
/>
<ListView
android:id="@+id/listNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>

listrow.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">
<TextView
android:name="@+id/listTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|left"
android:textSize="18dip"
android:textStyle="bold"
android:text="TextView">
</TextView>
<TextView
android:name="@+id/listDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:textSize="12dip"
android:textStyle="bold"
android:textColor="@android:color/white"
android:text="TextView">
</TextView>
</LinearLayout>

最佳答案

您永远不会为 titleView 分配任何内容。

您需要在 super.onCreate() 之后在您的 onCreate() 中执行以下操作

titleView = (TextView) this.getViewById(R.id.listTitle);

确保将 titleView 声明为类(class)顶部的一个字段,以便类(class)的其他人可以在需要时访问它。

希望这对您有所帮助!

编辑:

我刚刚注意到的重要说明:

android:name="@+id/myName" 

不同
android:id="@+id/myName"

您需要确保声明了 id,否则您将无法访问布局元素。

关于Android,ListView测试代码上的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com