[英]Getting Null Pointer Exception error
我从这样的XML文件中获取数据:
<posts>
<page id="1">
<title>
<![CDATA[ Tile no. 1 ]]>
</title>
<smallimage>
http://www.xyz.com/right.jpg
</smallimage>
</page>
<page id="2">
<title>
<![CDATA[ Tile no. 2 ]]>
</title>
</page>
<page id="3">
<title>
<![CDATA[ Tile no. 3 ]]>
</title>
<smallimage>
http://www.xyz.com/left.jpg
</smallimage>
</page>
</posts>
这是我的XML的示例,其中我正在获取元素,并且我在单个URL中获得10个页面,其中存在10个而不是10个预设。 可以是3或6或1。
在我的代码中,我正在使用SAX解析器来解析此XML文件,我正在获取我的数据并能够在列表视图中显示,但是我可以与它们一起显示图像。
为了显示图像,我使用的是LazlyList示例(ImageLoader类)。
这是我的代码如下:-
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
static final String URL = "http://www.xyz.com/api.php?page_id=1";
ItemList itemList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
XMLParser parser = new XMLParser();
String XML = parser.getXmlFromUrl(URL);
System.out.println("This XML is ========>"+XML);
try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Create handler to handle XML Tags ( extends DefaultHandler ) */
MyXMLHandler myXMLHandler = new MyXMLHandler();
xr.setContentHandler(myXMLHandler);
ByteArrayInputStream is = new ByteArrayInputStream(XML.getBytes());
xr.parse(new InputSource(is));
}
catch(Exception e)
{
}
itemList = MyXMLHandler.itemList;
ArrayList<String> listItem= itemList.getTitle();
ArrayList<String> listManu = itemList.getSmallimages();
System.out.println("(ListItem)=======>"+listItem);
System.out.println("(ListManu)=======>"+listManu);
// String ar[];
Object obj[]=listManu.toArray();
String[] stringArray=Arrays.asList(obj).toArray(new String[obj.length]);
System.out.println("Array to String"+Arrays.toString(stringArray));
// ar=(String[]) listManu.toArray();
ListView lview = (ListView) findViewById(R.id.listview1);
myAdapter adapter = new myAdapter(this, listItem, stringArray);
lview.setAdapter(adapter);
}
myAdapter.java
public class myAdapter extends BaseAdapter
{
ArrayList<String> listTitle;
String [] data;
ImageLoader imageLoader;
Activity activity;
public myAdapter(Activity activity, ArrayList<String> listTitle, String[] d) {
super();
this.listTitle = listTitle;
this.data = d;
this.activity = activity;
this.imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
// TODO Auto-generated method stub
return data.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder
{
TextView txtViewTitle;
ImageView image;
}
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder title;
LayoutInflater inflater = activity.getLayoutInflater();
if(view==null)
{
view = inflater.inflate(R.layout.lview_row, null);
title = new ViewHolder();
title.txtViewTitle = (TextView) view.findViewById(R.id.txtItem);
title.image = (ImageView) view.findViewById(R.id.list_image);
view.setTag(title);
}
else
{
title = (ViewHolder) view.getTag();
}
title.txtViewTitle.setText(listTitle.get(position));
imageLoader.DisplayImage(data[position], title.image);
return view;
}
}
\\
当我使用return data.length时,我得到的图像和瓷砖只没有。 存在图片,因为String []仅存储图片而不存储空格...
public int getCount() {
return data.length;
但是当我将data.length更改为listTitle.size()时
public int getCount() {
return listTitle.size();
得到空指针异常我正在使用listTilte.size(),因为它给出了完整列表...
任何人都可以建议我如何解决此问题并在列表视图中获取图像和完整标题数据以及存在图像的位置,只有我将获得带有标题的图像,否则仅标题会显示在列表视图中...
先谢谢了。
如果使用listTitle.size();
时得到nullpointerexception listTitle.size();
然后listTitle不会初始化。
除了在getter中,您的代码示例不包含listTitle的代码。
在不查看MyXMLHandler的源代码的情况下很难帮助您。 使用XML解析确实是一件痛苦的事情,您正在获取数据,您还控制服务器生成数据吗? 如果是这样,我强烈建议您改为通过JSON发送...
JSON是一种以类似于javascript的语法编写对象的方式,该语法很容易通过机器解析,您的列表看起来可能像这样:
{"posts:":
[
{"pageId": 1, "title":"sometitle", "smallImage":"someimage.png"},
{"pageId": 2, "title":"sometitle", "smallImage":"someimage.png"},
{..... etc }
]
}
要用PHP创建它,您要做的就是:
class PostsList {
var $posts = array();
}
class Post {
var $pageId;
var $title;
var $smallImage;
function Post($pageId, $title, $smallImage) {
$this->pageId = $pageId; $this->title = $title; $this->smallImage = $smallImage;
}
}
$posts = new PostsList();
$posts->posts[] = new Post(1, "some title", "image.png");
$posts->posts[] = new Post(2, "some other title", "image2.png");
echo json_encode($posts);
现在,在Java中,您将创建相同的对象:
class PostsList {
public ArrayList<Post> posts;
}
class Post {
public int pageId;
public String title;
public String smallImage;
}
然后,您将获得GSON(可在android中使用),然后执行以下操作:
Gson gson = new GsonBuilder().create();
String jsonData = getDataFromServer();
PostsList gotFromServer = gson.fromJson(jsonData, PostsList.class);
瞧! 您已经将所有帖子都解析为java对象,甚至可以将PostsList对象变成适配器以供list使用...
在我对JSON变得更加熟悉之后,我停止使用XML进行RPC,JSON数据非常容易使用,以后添加或删除字段也非常容易...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.