使用Parcelable </string>传递List <string>

时间:2013-11-12 14:21:28

标签: android json android-asynctask parcelable

我的parcelable有问题。我认为几乎一切都还可以,但它仍然无效,我不知道为什么但这里是我的代码

categoriasParcel

public class categoriasParcel implements Parcelable{

public List<String> categorias;

public categoriasParcel(List<String> lista)
{
    super();
    this.categorias = lista;
}

protected categoriasParcel(Parcel in) {
    categorias = new ArrayList<String>();
    in.readList(categorias, null);
}

public List<String> getCategorias()
{
    return categorias;
}
public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeList(categorias);
}

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

    public categoriasParcel[] newArray(int size) {
            return new categoriasParcel[size];
            }
        };
}

在我的活动1中,我有一个asynctask

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(InicioActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            if(json != null){
                JSONArray datos = json.getJSONArray("datos");
                List<String> lista = new ArrayList<String>();
                for (int i = 0; i < datos.length(); i++) {
                    JSONObject jsonObject = datos.getJSONObject(i);
                    lista.add(jsonObject.getString("nombre"));
                }
                categoriasParcel cat = new categoriasParcel(lista);

                Intent intent = new Intent (getBaseContext(), PrincipalActivity.class);
                intent.putExtra("categorias",cat);
                startActivity(intent);

            }

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

    }
}

这个,获取一个JSON并将每个值放在一个List中(获取带有值的JSON工作,所以没有问题)

这是我的活动2

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_principal);

    Intent intent = getIntent();

    categoriasParcel parcelableLaptop = (categoriasParcel) intent
            .getParcelableExtra("categorias");
    List<String> laptop = parcelableLaptop.getCategorias();

    String[] c = laptop.toArray(new String[laptop.size()]);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.lista = c;
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

我不知道该如何处理,我尝试了很多方法,但我总是得到一个NullPointer

这是错误

11-12 10:40:13.500    3457-3457/com.prueba.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prueba.test/com.prueba.test.PrincipalActivity}: android.view.InflateException: Binary XML file line #33: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
        at android.app.Activity.setContentView(Activity.java:1881)
        at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
        at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
        at com.prueba.test.PrincipalActivity.onCreate(PrincipalActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException: storage == null
        at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
        at java.util.Arrays.asList(Arrays.java:154)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:141)
        at com.prueba.test.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:106)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
        at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
        at android.app.Activity.setContentView(Activity.java:1881)
        at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
        at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
        at com.prueba.test.PrincipalActivity.onCreate(PrincipalActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5104)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5041)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511

1 个答案:

答案 0 :(得分:1)

categoriasParcel(Parcel)构造函数上,您将在classloader参数中发送null

protected categoriasParcel(Parcel in) {
    categorias = new ArrayList<String>();
    in.readList(categorias, categoriasParcel.class.getClassLoader());
}

另一个建议:由于您使用的是List<String>,因此您可以使用Parcel.readStringList()和Parcel.writeStringList()将列表读/写为Parcel。