iT邦幫忙

0

android Activity傳值到tab fragment 編譯錯誤"空值"

  • 分享至 

  • xImage

錯誤代碼

 Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
 at com.example.useweb0204.tab1.onCreateView(tab1.java:54)
  tab1:54是這條錯誤
  String test1 = bundle.getString("DATA");

這是我輸入的地方 我很確定我的string name這邊有值

                    String name=jsonData.getString("id");
                    String email=jsonData.getString("password");
                    String phone=jsonData.getString("user_phone");
                    Intent intent= new Intent();
                    Bundle bundle = new Bundle();
                    tab1 myFragment = new tab1();
                    //bundle.putString("user_id",name);//傳遞Double
                    //bundle.putString("user_phone",id_phone);//傳遞Double
                    /*往bundle中添加数据*/
                    //bundle.putInt("index",index);
                    bundle.putString("test1", name);
                    /*把数据设置到Fragment中*/
                    myFragment.setArguments(bundle);
                    intent.setClass(Login.this, home.class);
                    //intent.putExtras(bundle);
                    startActivity(intent);
                    finish();

tab1fragment 我想在這邊接收值

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab1, container, false);
        error_id = view.findViewById(R.id.error);
        Bundle bundle = this.getArguments();
        String test1 = bundle.getString("test1");
        error_id.setText(test1.toString());
        return view;
    }

這是我的home.class

public class home extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
    }
    private BottomNavigationView mBv;
    private ViewPager mVp;
    private void initView() {
        mBv = (BottomNavigationView) findViewById(R.id.bv);
        mVp = (ViewPager) findViewById(R.id.vp);
        mBv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mVp.setCurrentItem(0);
                        return true;

                    case R.id.navigation_love:
                        mVp.setCurrentItem(1);
                        return true;

                    case R.id.navigation_shop_cart:
                        mVp.setCurrentItem(2);
                        return true;

                    case R.id.navigation_search:
                        mVp.setCurrentItem(3);
                        return true;
                }
                return false;
            }
        });

        //数据填充
        setupViewPager(mVp);
        //ViewPager监听
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mBv.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //停止滑動
//                mVp.setOnTouchListener(new View.OnTouchListener() {
//                            @Override
//                            public boolean onTouch(View v, MotionEvent event) {
//                                return true;
//                            }
//                        });
    }
    private void setupViewPager(ViewPager viewPager) {
        BottomAdapter adapter = new BottomAdapter(getSupportFragmentManager());
        adapter.addFragment(new tab1());
        adapter.addFragment(new tab2());
        adapter.addFragment(new tab3());
        adapter.addFragment(new tab4());
        viewPager.setAdapter(adapter);
    }
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
神威
iT邦研究生 4 級 ‧ 2020-02-21 08:16:55
最佳解答

請把你的tabfragment改這樣試試

 Intent intent = this.getIntent();
        Bundle bundle=intent.getExtras();
        String test1 = bundle.getString("test1");

我成功了感恩

我要發表回答

立即登入回答