我有一个活动,我正在尝试对 url 进行 Web 服务调用并将 json 响应显示到活动中,但是一旦我进行该活动,我的应用程序就会强制停止。
我的 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>