getArguments抛出NullPointerException

时间:2015-06-03 20:07:21

标签: java android nullpointerexception bundle

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

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.access$800(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.access$800(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 bundle?

2 个答案:

答案 0 :(得分:1)

您要添加FragmentList两次:

  • 第一次从静态XML中膨胀
  • 第二次使用FragmentTransaction
  • 动态添加另一个

你添加的第一个FragmentList没有参数 - 没有办法将参数传递给从静态XML中膨胀的片段,而这个片段会抛出异常。

我怀疑您要找的是用Fragment标记替换acitvity_main.xml中的FrameLayout标记 - FrameLayout是一个可以保存动态添加的片段的容器。< / p>

答案 1 :(得分:0)

检查Fragment中的getArguments(),它返回null。