将ArrayList <string>从活动传递到android </string>中的片段

时间:2015-03-31 21:12:03

标签: java android arraylist

我一直处理一个问题,现在我似乎无法弄明白,我试图将ArrayList<String>从一个活动传递给一个片段,但是,当我获得以下堆栈跟踪时运行代码

03-30 19:26:38.465  28955-28955/org.tcnjteamandroid.cryptochat E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: org.tcnjteamandroid.cryptochat, PID: 28955
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.tcnjteamandroid.cryptochat/org.tcnjteamandroid.cryptochat.drawer}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5240)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at org.tcnjteamandroid.cryptochat.drawer.onCreate(drawer.java:53)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5240)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getStringArrayList(java.lang.String)' on a null object reference
            at org.tcnjteamandroid.cryptochat.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:96)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
            at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
            at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
            at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
            at org.tcnjteamandroid.cryptochat.drawer.onCreate(drawer.java:53)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2361)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1279)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5240)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我用来传递这些信息并将其提取的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);

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

    ArrayList<String> menuItems = new ArrayList<String>();
    menuItems.add("Test");

    Bundle bundle = new Bundle();
    bundle.putStringArrayList("edttext", menuItems);
    /* set Fragmentclass Arguments */
    NavigationDrawerFragment fragobj = new NavigationDrawerFragment();
    fragobj.setArguments(bundle);

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

}

我用来获取新参数的代码是:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ArrayList<String> strtext = getArguments().getStringArrayList("edttext");
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                strtext));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

我将非常感谢能得到的任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用具有静态ArrayList属性的类

public class Values
{
    public static ArrayList<String> myArrayList;
}
活动中的

ArrayList<String> menuItems = new ArrayList<String>();
Values.myArrayList = menuItems;
片段中的

ArrayList<String> strtext = Values.myArrayAdapter;