getArguments 抛出 NullPointerException

getArguments throws NullPointerException

我在向片段发送值时遇到问题。

public class ActivityMain extends ActionBarActivity {

int currentPage = 1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Bundle bundle = new Bundle();
    textViewPages = (TextView) findViewById(R.id.text_current_page);
    if(isOnline()) {
        //here will be asyncTask
        bundle.putInt("page", currentPage );
        fragmentList = new FragmentList();
        fragmentList.setArguments(bundle);
        fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_main, fragmentList);
        fragmentTransaction.commit();
    }
    else {
        Toast.makeText(this, getResources().getString(R.string.empty_list), Toast.LENGTH_LONG).show();
    }

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}
}

答案可能很简单,但我找不到。

public class FragmentList extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        currentPage = bundle.getInt("page");
    }

getInt() 总是抛出 NullPointerException,就像在 onActivityCreated 或 onCreateView 中一样。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment_main"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="0.9"
        android:name="com.example.testex.reader.FragmentList"
        tools:layout="@layout/fragment_list">
    </fragment>

</LinearLayout>

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

有点logcat

06-03 23:57:34.355  25337-25337/com.example.testex.reader E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.testex.reader, PID: 25337
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testex.reader/com.example.testex.reader.ActivityMain}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.testex.reader.FragmentList.onActivityCreated(FragmentList.java:46)
            at android.app.Fragment.performActivityCreated(Fragment.java:1708)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
            at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1853)
            at android.app.Activity.performCreate(Activity.java:5346)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

我应该如何更改此代码以在片段中获取 !null 包?

检查 Fragment 中的 getArguments() ,它 returns 为空。

您要添加 FragmentList 两次:

  • 第一次从静态充气XML
  • 第二次使用FragmentTransaction
  • 动态添加另一个

您添加的第一个 FragmentList 没有参数 - 无法将参数传递给从静态 XML 膨胀的片段,正是这个片段引发了异常。

我怀疑您正在寻找的是用 FrameLayout 标签替换 acitvity_main.xml 中的 Fragment 标签 - FrameLayout 是一个容器,可以容纳动态添加的片段.