簡體   English   中英

通過意圖將編輯文本的文本移動到另一個活動會引發異常

[英]moving the text of an edit text through an intent to another activity is throwing an exception

我正在嘗試在我的Android應用中實現搜索功能,我想從活動發現中獲取用戶在搜索編輯文本中寫的內容,然后打開一個稱為活動搜索的新活動,其中可以顯示結果,發布對象是實現的類,每次嘗試從后端獲取數據時,這里都會拋出異常,這是這部分活動發現的代碼。

search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            // setContentView(R.layout.search_grid);
            // gridView2 = (GridView) findViewById(R.id.search_grid);
            RequestParams rp = new RequestParams();
            final String url;
            if (people.isChecked()) {
                rp.add("user", search.getText().toString());
                rp.add("counter", "");
                url = "search_users";
            } else if (tags.isChecked()) {
                rp.add("tag", search.getText().toString());
                rp.add("counter", "");
                url = "search_exact_hashtag";
            } else {
                rp.add("location", search.getText().toString());
                rp.add("counter", "");
                url = "search_location";
            }
            final ArrayList<PostObject> addyExtras = new ArrayList<>();
            //Toast.makeText(getApplicationContext()," something " + , Toast.LENGTH_SHORT).show();
            PabloClient.post(url, rp, new JSONHandler(getApplicationContext(), new JSONHandler.OnFinishedListener() {
                @Override
                public void OnFinished(int statusCode, Header[] headers, JSONObject response, int result) {
                    try {
                        if (result == 1) {
                            // Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();

                            JSONArray posts = response.getJSONArray("data");
                            setContentView(R.layout.search_grid);
                            //////////////////////////////////////////////


                            //////////////////////////////////////////////////////////////////
                            for (int i = 0; i < posts.length(); i++) {
                                searchPosts.add(new PostObject(posts.getJSONObject(i)));


                                addyExtras.add(searchPosts.get(i));

                            }




                        }
                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                    Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
                    intent.putParcelableArrayListExtra("mylist", addyExtras);
                    startActivity(intent);

                }
            }));
        }

    });

`

這就是我如何獲得搜索活動的意圖

searchPosts2 = getIntent().getParcelableArrayExtra("mylist");

在PostObject類中執行以下操作。

   public class PostObject implements Parcelable 
   {

      ///......your class code


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

}

public PostObject(Parcel in) {
    super();
//    readFromParcel(in);
}

public static final Parcelable.Creator<PostObject> CREATOR = new Parcelable.Creator<PostObject>() {
    public PostObject createFromParcel(Parcel in) {
        return new PostObject(in);
    }

    public PostObject[] newArray(int size) {

        return new PostObject[size];
    }

};

}

數組應該在Bundle的幫助下通過

發送數組

首先從發送方開始,您需要在捆綁包中添加數組列表

Bundle data = new Bundle();
data.putParcelableArrayList("mylist", addyExtras);

然后將包添加到Intent

 Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
    intent .putExtra("content", data);
    startActivity(yourIntent);

檢索數組

在接收端從意圖中獲得捆綁

Bundle data =  getIntent().getBundleExtra("content");

然后從包中獲取數組列表

searchPosts2  =  data.getParcelableArrayList("mylist");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM