我有一项活动,其中我正在尝试为网址服务,并将json响应显示在活动中,但我的应用程序会在我继续该活动时立即停止。
My RedemptionActivity是这样的:
public class RedemptionActivity extends Activity {
ListView redemptionListView;
RedemptionListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.redemption);
redemptionListView = (ListView) findViewById(R.id.redemptionListView);
if (SnapDineCommon.getInstance(this).isNetworkAvailable()) {
new RedemptionResultAsyncTask(this).execute();
} else
Toast.makeText(this, getString(R.string.check_network),
Toast.LENGTH_SHORT).show();
}
public class RedemptionResultAsyncTask extends
AsyncTask<String, Void, Boolean> {
Context context;
ProgressDialog progressDialog;
RedemptionResultResponse redemptionResultResponse;
public RedemptionResultAsyncTask(Context context) {
this.context = context;
}
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
try {
Gson gson = new Gson();
RestWebServiceClient client = new RestWebServiceClient(
SnapDineCommon.getInstance(this.context).SiteUrl
+ "getRedemptionData", this.context);
client.AddParam("mobile_number",
SnapDineCommon.getInstance(context)
.getLoggedinMobileNumber());
client.Execute(RequestMethod.GET);
String response = client.getResponse();
if (response == null)
return false;
else {
redemptionResultResponse = gson.fromJson(response,
RedemptionResultResponse.class);
if (redemptionResultResponse == null)
return false;
else
return true;
}
} catch (Exception e) {
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
progressDialog = null;
if (result) {
if (redemptionResultResponse.Success) {
if (redemptionResultResponse.getSearchResult().size() > 0) { //throwing null pointer exception
adapter = new RedemptionListAdapter(context,
R.layout.searchresult_row,
redemptionResultResponse.getSearchResult());
redemptionListView.setAdapter(adapter);
} else {
Intent intent = new Intent(context,
GenericOkDialogActivity.class);
startActivity(intent);
}
} else {
Toast.makeText(context, redemptionResultResponse.errorMessage,
Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, "An error occured. Please try later.",
Toast.LENGTH_SHORT).show();
}
}
}
}
和logcat显示以下错误:
04-10 14:04:21.545: E/AndroidRuntime(6798): FATAL EXCEPTION: main
04-10 14:04:21.545: E/AndroidRuntime(6798): java.lang.NullPointerException
04-10 14:04:21.545: E/AndroidRuntime(6798): at com.JustDine.GUI.RedemptionActivity$RedemptionResultAsyncTask.onPostExecute(RedemptionActivity.java:101)
04-10 14:04:21.545: E/AndroidRuntime(6798): at com.JustDine.GUI.RedemptionActivity$RedemptionResultAsyncTask.onPostExecute(RedemptionActivity.java:1)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.os.AsyncTask.finish(AsyncTask.java:417)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.os.AsyncTask.access$300(AsyncTask.java:127)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.os.Looper.loop(Looper.java:123)
04-10 14:04:21.545: E/AndroidRuntime(6798): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-10 14:04:21.545: E/AndroidRuntime(6798): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 14:04:21.545: E/AndroidRuntime(6798): at java.lang.reflect.Method.invoke(Method.java:521)
04-10 14:04:21.545: E/AndroidRuntime(6798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
04-10 14:04:21.545: E/AndroidRuntime(6798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
04-10 14:04:21.545: E/AndroidRuntime(6798): at dalvik.system.NativeStart.main(Native Method)
04-10 14:04:21.555: W/ActivityManager(124): Force finishing activity com.JustDine.GUI/.RedemptionActivity
04-10 14:04:21.555: W/AppErrorDialog(124): xjzb001 AppErrorDialog setLongMsg=java.lang.NullPointerException
我无法弄清楚为什么会意外关闭。请帮忙,为什么"redemptionResultResponse.getSearchResult().size()"
为空,我已确认redemptionResultResponse不为空.. ?????
更新:以下是我在RedemptionResultResponse中的内容:
@SerializedName("Success")
public boolean Success;
@SerializedName("GetSearchResult")
private List<SearchResult> getSearchResult;
@SerializedName("errorMessage")
public String errorMessage;
@SerializedName("Tag")
public String Tag;
public List<SearchResult> getSearchResult() {
return getSearchResult;
}
public void setSearchResult(List<SearchResult> getSearchResult) {
this.getSearchResult = getSearchResult;
}
更新了XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LoginScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/realtiveLayout1"
android:layout_width="fill_parent"
android:layout_height="44dp"
android:background="#ffffff" >
<ImageButton
android:id="@+id/ibSearchResultsBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="5dip"
android:background="@drawable/backbtn" />
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Search Result"
android:textColor="#FF0000"
android:textSize="22sp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/settingLayout"
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:gravity="center" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/settingicon" />
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="@drawable/light_red_gradient" />
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search.."
android:singleLine="true" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8" >
<ListView
android:id="@+id/redemptionListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="15dp"
android:background="@drawable/light_red_gradient" />
</LinearLayout>
答案 0 :(得分:0)
当您在dismiss()
上致电progressDialog
时,您基本上是使用GC
将其从内存中删除,您无需致电progressDialog = null;
如果由于if (redemptionResultResponse.getSearchResult().size() > 0)
导致NullPointerException
崩溃,那么我的猜测是getSearchResult()
会返回NULL
。
答案 1 :(得分:0)
向我们展示您的XML文件...由于您的Xml文件包含一些错误,会发生此类错误。空指针异常显示您请求的URL未返回任何内容..Refer {{3} } ..
答案 2 :(得分:0)