将值从Activity传递到Fragment时出现空指针异常

时间:2016-02-23 19:16:44

标签: java android

这是我的MainActivity类。从这个类,我试图将数据发送到我的片段'HomeFragment',然后启动第二个活动'SwipeMainActivity'。

enter image description here

这是编码的一部分:

 @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Title clickedTitle = mAdapter.getItem(position);
            Log.d(TAG, "clickedItem : " + clickedTitle.getTitleName());
            Intent intent = new Intent(this, SwipeMainActivity.class);
            HomeFragment fragobj=new HomeFragment();
            Bundle bundle = new Bundle();
            bundle.putLong(HomeFragment.EXTRA_SELECTED_TITLE_ID, clickedTitle.getId());
            fragobj.setArguments(bundle);
    //       intent.putExtra(SwipeMainActivity.EXTRA_SELECTED_TITLE_ID, clickedTitle.getId());
            startActivity(intent);

这是我的SwipeMainActivity Class

    public class SwipeMainActivity extends AppCompatActivity {
        Toolbar toolbar;
        TabLayout tabLayout;
        ViewPager viewPager;
        ViewPagerAdapter viewPagerAdapter;
        private String mTitleId;
        public static final String EXTRA_SELECTED_TITLE_ID = "extra_key_selected_title_id";

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

            toolbar = (Toolbar) findViewById(R.id.toolBar);
            setSupportActionBar(toolbar);


            tabLayout=(TabLayout) findViewById(R.id.tabLayout);
            viewPager= (ViewPager) findViewById(R.id.viewPager);
            viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFragment(new HomeFragment(),"HOME");
            viewPagerAdapter.addFragment(new TopFreeFragment(),"Free");
            viewPagerAdapter.addFragment(new TopPaidFragment(),"Paid");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);

        }
    }

然后,我的HomeFragment:

enter image description here

 public class HomeFragment extends ListFragment implements AdapterView.OnItemLongClickListener, AdapterView.OnItemClickListener,View.OnClickListener{

    public static final String EXTRA_SELECTED_TITLE_ID = "extra_key_selected_title_id";
    Long i;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Bundle bundle = getArguments();
//        if (bundle != null) {

            i = bundle.getLong(this.EXTRA_SELECTED_TITLE_ID, -1);
//        }
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

我收到了这个错误:

02-24 00:34:02.266 15575-15575/com.kawlni.silence.bekang W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-24 00:34:02.276 15575-15575/com.kawlni.silence.bekang E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
        at com.kawlni.silence.bekang.Swipe.HomeFragment.onCreateView(HomeFragment.java:58)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
        at android.view.View.measure(View.java:8313)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您可以使用此工厂方法方法使用提供的参数创建片段的新实例。

public static final String EXTRA_SELECTED_TITLE_ID = "extra_key_selected_title_id";

private Long mTitleId;


public static HomeFragment newInstance(Long titleId) {
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(EXTRA_SELECTED_TITLE_ID, titleId);
    fragment.setArguments(args);
    return fragment;
}

public HomeFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mTitleId = getArguments().getString(EXTRA_SELECTED_TITLE_ID);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {    
    Log.d(LOG_TAG , "EXTRA_SELECTED_TITLE_ID = " + mTitleId);
    return inflater.inflate(R.layout.fragment_home, container, false);  
}

您可以使用以下方法:

HomeFragment fragobj = HomeFragment.newInstance(clickedTitle.getId());