在webservice类中,它将从在线检索数据库并放入列表中:
public List<List_NewsComment> allCommentList;
public void GetCommentNews( final int gCommentNewsID)
{
Thread networkThread=new Thread(){
@Override
public void run(){
try
{
SoapObject request= new SoapObject(NAMESPACE,get_Comment_METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
HttpTransportSE androidHttpTransportSE= new HttpTransportSE(URL,60000);
request.addProperty("itemid", gCommentNewsID);
envelope.setOutputSoapObject(request);
androidHttpTransportSE.call(get_Comment_SOAP_ACTION, envelope);
SoapObject result=(SoapObject)envelope.bodyIn;
RetrieveFromSoap( result);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
networkThread.start();
}
public List<List_NewsComment> RetrieveFromSoap(SoapObject soap)
{
allCommentList= new ArrayList<List_NewsComment>();
Vector<Object> property2 = extracted(soap);
for (int i = 0; i< property2.size();i++){
SoapObject getPropertyD=(SoapObject)property2.get(i);
List_NewsComment addcomment= new List_NewsComment();
addcomment.setcommentDate(getPropertyD.getProperty("date").toString());
addcomment.setUserName( getPropertyD.getProperty("name").toString());
addcomment.setCommentContent(getPropertyD.getProperty("comment").toString());
allCommentList.add(addcomment);
}
webservice.allCommentList.size(); <-- can call here no problem
return allCommentList;
}
private static Vector<Object> extracted(SoapObject soap) {
return (Vector<Object>)soap.getProperty(0);
}
在Main Activity类中,我想检查列表的大小:
Database_WebService webservice = new Database_WebService(this);
webservice.GetCommentNews(newsid);
webservice.allCommentList.size(); <-- cannot call here, what is the problem
它返回nullpointerexception
。
有什么问题????
答案 0 :(得分:2)
制作List static
,以便您可以使用该类名称在任何地方访问它。
public static List<List_NewsComment> allCommentList;
Database_WebService.allCommentList.size();
正如大家建议在你的类中创建一个函数并返回该函数中的数组大小。请参阅下面的代码。
public int getCommentListSize()
{
return allCommentList.size();
}
then call this method as
Database_WebService databaseWebservice = new Database_WebService(this);
int size =databaseWebservice.getCommentListSize.size();
答案 1 :(得分:1)
它未初始化的原因是因为它在线程中被初始化,所以事件的顺序如下:
GetCommentNews
开始在线程执行完毕之前,您不能使用allCommentList
,所以在分配完成后,您需要在allCommentList
方法中用RetrieveFromSoap
发信号或做一些事情。
此外,您不应该将列表设置为静态,这违反了面向对象编程的原则,您应该通过getter公开allCommentList
而不是直接使用变量。
修改强>
实际上,离开Thread
并使用AsyncTask
可能会更好,此课程旨在让您更轻松地从Thread
开始工作。
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
答案 2 :(得分:0)
因为List allCommentList未初始化,所以它将在公共List RetrieveFromSoap(SoapObject soap)中初始化。
答案 3 :(得分:0)
public static List<List_NewsComment> allCommentList;
int size =Database_WebService.allCommentList.size();
--------------------------------------------------
我仍然建议不要好好使用静态来制作一个方法
public int getArrSize()
{
return allCommentList.size();
}
然后将此方法称为
Database_WebService dvws = new Database_WebService(this);
int size =dvws allCommentList.size();
答案 4 :(得分:0)
这非常难看,为什么不添加像'int webservice.getCommentsSize()'这样的方法并返回大小,这样你就可以测试列表是否为空并且你不需要直接访问。
像:
class Webservice{
private List allCommentList = null;
public int getCommentListSize(){
if(allCommentList==null) return 0;
return allCommentList.size();
}
}
在main中:
Webservice webservice = new Webservice();
int size = webservice.getCommentListSize();
另一个问题是,当你试图获得它的大小时,你的列表可能还没准备好。你为什么要在一个线程中运行它?你见过AsyncTask吗?
答案 5 :(得分:0)
你必须小心,因为你在一个不同的线程(networkThread)中创建你从哪里调用它(来自主线程)。在从外部访问networkThread之前,您应确定networkThread已完成。
答案 6 :(得分:0)
使ArrayList
静态。然后你可以使用类名来访问。你的其他类的ArrayList。