私はdata=null
Androidを初めて使用します。正しいonPostExecuteを実行すると取得します。次に、使用可能なデータがないことを示すダイアログを表示したいのですが、取得していNullPointerException
ます。
public class GetPreviousChatNewThread extends AsyncTask<Void,Void,Void> {
ProgressDialog dialog;
ArrayList<HashMap<String,String>> data;
@Override
protected Void doInBackground(Void... void1) {
data=new ArrayList<HashMap<String,String>>();
data=HandleJSON.ParseJsonForUserAdminChats(sendHttpRequest(
"ReturnUserAdminChats",
"admin",
"You" ,
"a",
clientEmail,
UserAdminChatActivity.LastShowingChatDate));
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(!data.isEmpty()) // <-- error here
{
adapter.InsertValuesAtTop(data);
}
else
{
Toast.makeText(getApplicationContext(), "No more data", Toast.LENGTH_LONG).show();
}
if(dialog.isShowing())
dialog.dismiss();
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(UserAdminChatActivity.this, "", "Loading. Please wait...", true);
}
}
これが私のLogcatで、プロセスの完全なログを示しています。
07-23 12:22:55.976: I/finalAnswer(25159): ["a@hotmail.com","c@hotmail.com","b@hotmail.com"]
07-23 12:23:01.101: I/finalAnswer(25159): [{"username":"moji","chatText":"jingalala hu","chatDate":"4\\July\\2012"},{"username":"You","chatText":"bingalala hu","chatDate":"4\\July\\2012"},{"username":"moji","chatText":"jingalala hu","chatDate":"4\\July\\2012"}]
07-23 12:23:01.101: I/GoTo Json(25159): [{"username":"moji","chatText":"jingalala hu","chatDate":"4\\July\\2012"},{"username":"You","chatText":"bingalala hu","chatDate":"4\\July\\2012"},{"username":"moji","chatText":"jingalala hu","chatDate":"4\\July\\2012"}]
07-23 12:23:01.101: I/dateeeeeee(25159): 4\July\2012
07-23 12:23:01.101: I/dateeeeeee(25159): 4\July\2012
07-23 12:23:01.111: I/dateeeeeee(25159): 4\July\2012
07-23 12:23:01.161: I/finalAnswer(25159): new"[]"
07-23 12:23:02.603: I/finalAnswer(25159): []
07-23 12:23:02.603: I/GoTo Json(25159): []
07-23 12:23:02.613: D/AndroidRuntime(25159): Shutting down VM
07-23 12:23:02.613: W/dalvikvm(25159): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
07-23 12:23:02.623: E/AndroidRuntime(25159): FATAL EXCEPTION: main
07-23 12:23:02.623: E/AndroidRuntime(25159): java.lang.NullPointerException
07-23 12:23:02.623: E/AndroidRuntime(25159): at com.app.ServerClient.UserAdminChatActivity$GetPreviousChatNewThread.onPostExecute(UserAdminChatActivity.java:197)
07-23 12:23:02.623: E/AndroidRuntime(25159): at com.app.ServerClient.UserAdminChatActivity$GetPreviousChatNewThread.onPostExecute(UserAdminChatActivity.java:1)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.os.Looper.loop(Looper.java:138)
07-23 12:23:02.623: E/AndroidRuntime(25159): at android.app.ActivityThread.main(ActivityThread.java:3701)
07-23 12:23:02.623: E/AndroidRuntime(25159): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 12:23:02.623: E/AndroidRuntime(25159): at java.lang.reflect.Method.invoke(Method.java:507)
07-23 12:23:02.623: E/AndroidRuntime(25159): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
07-23 12:23:02.623: E/AndroidRuntime(25159): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
07-23 12:23:02.623: E/AndroidRuntime(25159): at dalvik.system.NativeStart.main(Native Method)
編集:
HandleJsonのコードは次のとおりです。
public class HandleJSON {
private static ArrayList<HashMap<String,String>> chat;
static final String Key_username="username_key";
static final String Key_email="email_key";
static final String Key_messageText="messageText_key";
static final String Key_messageDate="messageDate_key";
static final String Key_messageCounts="messageCount_key";
static boolean newObject;
public static ArrayList<HashMap<String,String>> ParseJsonForLatestChats(String jsonData)
{
try {
if(HandleJSON.newObject)
{
chat=new ArrayList<HashMap<String,String>>();
}
Log.i("jsonReturned",jsonData.toString());
JSONArray jsonarr=new JSONArray(jsonData);
for(int i=0;i<jsonarr.length();i++)
{
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(HandleJSON.Key_username, jsonarr.getJSONObject(i).getString("username"));
hashMap.put(HandleJSON.Key_email, jsonarr.getJSONObject(i).getString("email"));
hashMap.put(HandleJSON.Key_messageCounts, jsonarr.getJSONObject(i).getString("count"));
chat.add(hashMap);
}
return chat;
}
catch(JSONException e)
{
Log.e("JsonDek", e.getMessage());
return null;
}
}
public static ArrayList<HashMap<String,String>> ParseJsonForUserAdminChats(String jsonData)
{
chat=new ArrayList<HashMap<String,String>>();
try {
JSONArray jsonarr=new JSONArray(jsonData);
Log.i("GoTo Json",jsonData);
UserAdminChatActivity.LastShowingChatDate=jsonarr.getJSONObject(0).getString("chatDate");
for(int i=jsonarr.length()-1;i>=0;i--)
{
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put(HandleJSON.Key_username, jsonarr.getJSONObject(i).getString("username"));
hashMap.put(HandleJSON.Key_messageText, jsonarr.getJSONObject(i).getString("chatText"));
hashMap.put(HandleJSON.Key_messageDate, jsonarr.getJSONObject(i).getString("chatDate"));
Log.i("dateeeeeee",jsonarr.getJSONObject(i).getString("chatDate"));
chat.add(hashMap);
}
return chat;
}
catch(JSONException e)
{
return null;
}
}
public static ArrayList<String> ReturnUsersEmail(String jsonData) throws JSONException
{
JSONArray jsonarr=new JSONArray(jsonData);
ArrayList<String> emails=new ArrayList<String>();
for(int i=0;i<jsonarr.length();i++)
{
emails.add(jsonarr.getString(i));
}
return emails;
}
}