0

我想把一个 Bundle 放在一个 Fragment 中。此捆绑包必须存在于 onCreate 方法中。但我得到一个空指针异常。

Activity中的代码部分

Bundle bundl = new Bundle();
String headlines[]  = {"sdfsdf","sdfsd","fgfg","sdffsd"};
bundl.putStringArray("123", headlines);
bundl.putStringArray("1253", headlines);
HeadlinesFragment firstFragment = new HeadlinesFragment();
firstFragment.setArguments(bundl); 

onCreate 方法

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Here is the null pointer
    headlines =getArguments().getStringArray("123");

    int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
            : android.R.layout.simple_list_item_1;
    inserts = new ArrayAdapter<String>(getActivity(), layout, headlines);

    setListAdapter(inserts);
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment android:name="com.de.ordix.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.de.ordix.ArticleFragment"
          android:id="@+id/abwesend_fragment"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

这里是错误日志

E/AndroidRuntime(25007): FATAL EXCEPTION: main
E/AndroidRuntime(25007): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.de.xx/com.de.xx.MainActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
E/AndroidRuntime(25007): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(25007): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(25007): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(25007): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(25007): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25007): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(25007): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(25007): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25007): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(25007): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(25007): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(25007): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25007): Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
E/AndroidRuntime(25007): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
E/AndroidRuntime(25007): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
E/AndroidRuntime(25007): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
E/AndroidRuntime(25007): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
E/AndroidRuntime(25007): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
E/AndroidRuntime(25007): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
E/AndroidRuntime(25007): at android.app.Activity.setContentView(Activity.java:1835)
E/AndroidRuntime(25007): at com.de.xx.MainActivity.onCreate(MainActivity.java:37)
E/AndroidRuntime(25007): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(25007): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(25007): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(25007): ... 11 more
E/AndroidRuntime(25007): Caused by: java.lang.NullPointerException
E/AndroidRuntime(25007): at com.de.xx.HeadlinesFragment.onCreate(HeadlinesFragment.java:39)
E/AndroidRuntime(25007): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:835)
E/AndroidRuntime(25007): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1061)
E/AndroidRuntime(25007): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1160)
E/AndroidRuntime(25007): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
E/AndroidRuntime(25007): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
E/AndroidRuntime(25007): ... 21 more
4

1 回答 1

0

声明一个类和静态变量

public class global {
  public static String headlines[] ;
}

现在你的代码

global.headlines =  {"sdfsdf","sdfsd","fgfg","sdffsd"};

当你想使用

inserts = new ArrayAdapter<String>(getActivity(), layout, global.headlines);
于 2012-07-25T09:33:11.867 回答