【问题标题】:Application getting force stop应用程序强制停止
【发布时间】:2013-04-10 08:45:28
【问题描述】:

我有一个活动,我正在尝试对 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>

【问题讨论】:

  • RedemptionActivity.java:101 的代码行是什么?有东西 null 你正在尝试使用它
  • 你得到 nullpointerexception 意味着某些东西是空的。检查行 RedemptionActivity.java:101
  • 然后getSearchResult() 返回空值。
  • 那么要么redemptionResultResponse 为空,要么redemptionResultResponse.getSearchResult() 返回空
  • 或 redemptionListView 为空

标签: android json exception nullpointerexception


【解决方案1】:

当您在progressDialog 上调用dismiss() 时,您基本上是使用GC 从内存中删除它,您不需要调用progressDialog = null;

如果if (redemptionResultResponse.getSearchResult().size() &gt; 0) 因NullPointerException 而崩溃,那么我的猜测是getSearchResult() 返回NULL。

【讨论】:

  • 如果 redemptionResultresponse 不为 null 那么 getSearchResult 怎么返回 null,我刚刚更新了它的声明..??
  • 我删除了progressDialog = null; 错误依旧。
  • 您确定 getSearchResult List 不为空吗,我没有在您的代码中看到您曾经使用 setSearchResult 方法设置它。
  • 我认为它不为空,而是我的列表视图为空,当我声明它时,它显示错误“redemptionListView 无法解析或不是字段”..我如何声明它?
  • 我已经声明它是 aa redemptionListView = (ListView) findViewById(R.id.redemptionListView); throws above error..
【解决方案2】:

向我们展示您的 XML 文件...由于您的 Xml 文件包含一些错误,将发生这种错误..空指针异常显示您请求的 URL 没有返回任何内容..请参阅 here。 .

【讨论】:

  • 更新了 XMl,但我认为它与 XML 无关。
【解决方案3】:

我发现了错误。错误是响应类中的 json,我没有正确解析 json 数据,从而引发了异常。

请参考here。

感谢大家的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多