获取null对象 - 将数据从一个活动发送到另一个活动

时间:2016-05-28 21:00:22

标签: java android api android-intent

我有一个实现serializable的类,我试图使用intent发送它的对象,所以当我使用putExtra()设置该对象时,我得到null对象;

以下是我的代码。

YelpSearch.java(fromClass)

import android.app.ListActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class YelpSearch extends ListActivity implements Serializable {

    @SuppressWarnings("serial")
    class Business implements Serializable {
        final String name;
        final String url;
        final String id;


        public Business(String name, String url, String id) {
            this.name = name;
            this.url = url;
            this.id = id;
        }


        @Override
        public String toString() {
            return name;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setTitle("Finding Tacos...");
        setProgressBarIndeterminateVisibility(true);
        Log.v("TAG","YelpSearchOnCreate");

        new AsyncTask<Void, Void, List<Business>>() {
            @Override
            protected List<Business> doInBackground(Void... params) {
                Log.v("TAG","Async List");

                String businesses = Yelp.getYelp(YelpSearch.this).search("food", "91754");
                Log.v("TAG","String businsesses"+businesses);
                try {
                    Log.v("TAG","try");
                    return processJson(businesses);
                } catch (JSONException e) {
                    return Collections.<Business>emptyList();
                }
            }

            @Override
            protected void onPostExecute(List<Business> businesses) {
                Log.v("TAG","onPostExecute");
                Log.v("BusinessesList","Businesses "+ businesses);
                //setTitle("Tacos Found");
                setProgressBarIndeterminateVisibility(false);
                getListView().setAdapter(new ArrayAdapter<Business>(YelpSearch.this, android.R.layout.simple_list_item_1, businesses));

            }
        }.execute();
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        Business biz = (Business) listView.getItemAtPosition(position);
        Log.v("Sending data to Detail","hi "+biz);
        //startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(biz.url)));
        Intent intent = new Intent(getApplicationContext(), YelpBizDetail.class);
       // Intent intent = new Intent(YelpSearch.this,YelpBizDetail.class).putExtra("myCustomerObj",biz);
        intent.putExtra("Detailclass",biz);

        Log.v("Sending data to Detail","sent "+biz);
        startActivity(intent);
    }

    List<Business> processJson(String jsonStuff) throws JSONException {
        JSONObject json = new JSONObject(jsonStuff);
        JSONArray businesses = json.getJSONArray("businesses");

        ArrayList<Business> businessObjs = new ArrayList<Business>(businesses.length());
        for (int i = 0; i < businesses.length(); i++) {
            JSONObject business = businesses.getJSONObject(i);
            businessObjs.add(new Business(business.optString("name"), business.optString("mobile_url"),business.optString("id")));
        }
        return businessObjs;
    }
}

YelpBizDetail(toClass)

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;


import com.....projectmaw.YelpSearch.Business;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


public class YelpBizDetail extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yelp_biz_detail);
        Intent i = getIntent();
        Business biz = (Business) i.getSerializableExtra("Detailclass");
       // String biz = (String) i.getSerializableExtra("Detailclass");

        //String biz = (String) i.getSerializableExtra("Detailclass");
        Log.v("Received on Detail","data "+biz);
    }



}

1 个答案:

答案 0 :(得分:2)

尝试在YelpSearch.java中对onListItemClick进行一些更改,例如更改startActivity(intent)      startActivity(new Intent(getApplicationContext(), YelpBizDetail.class).putExtra("Detailclass",biz));这将在新活动开始时创建新意图。并删除

Log.v("Sending data to Detail","hi "+biz);
        //startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(biz.url)));
        Intent intent = new Intent(getApplicationContext(), YelpBizDetail.class);
       // Intent intent = new Intent(YelpSearch.this,YelpBizDetail.class).putExtra("myCustomerObj",biz);
        intent.putExtra("Detailclass",biz);

        Log.v("Sending data to Detail","sent "+biz);