活动/片段生命周期导致NullPointerException

时间:2018-09-22 21:50:19

标签: java android android-fragments android-activity nullpointerexception

我正在尝试将数据从活动传递到片段。活动中的onStart方法调用片段内的getUID方法并传递String,另一端的片段接收String并设置TextView中的文本

问题在于,当活动调用片段中的方法时,片段为空:

堆栈跟踪:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.project / com.test.project.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void com.test.project.Chat .getUID(android.os.Bundle)'对空对象的引用           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)           在android.app.ActivityThread.access $ 900(ActivityThread.java:153)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1347)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:148)           在android.app.ActivityThread.main(ActivityThread.java:5451)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.test.project.Chat.getUID(android.os.Bundle)'           在com.test.project.MainActivity.onStart(MainActivity.java:178)           在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)           在android.app.Activity.performStart(Activity.java:6340)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2397)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)           在android.app.ActivityThread.access $ 900(ActivityThread.java:153)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1347)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:148)           在android.app.ActivityThread.main(ActivityThread.java:5451)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {

    MyClass myClass = new MyClass();


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fragmentManager.beginTransaction().replace(R.id.container, myClass).addToBackStack(null).commit();

    mAuth = FirebaseAuth.getInstance();

}

...

protected void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser == null ) {
        ...

    } else {

        String uid = currentUser.getUid();

        Chat chatFragment = (Chat)getSupportFragmentManager().findFragmentById(R.id.chat_layout);
        Bundle bundle = new Bundle();
        bundle.putString("uid", uid);
        chatFragment.getUID(bundle);

下面的此类创建一个ViewPager,该ViewPager使用ViewPagerAdapter填充tabLayout中的选项卡。

MyClass.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    final View rootView = inflater.inflate(R.layout.fragment_my_class, container, false);

    viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
    viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));

    tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    return rootView;
}

ViewPagerAdapter

public ViewPagerAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
    Log.e("error","Adapter");
    childFragments = new Fragment[]{
            new Chat(),
            new Reminders(),
            new Agenda()
    };

}

@Override
public Fragment getItem(int position) {
    Log.e("error", String.valueOf(position));
    return childFragments[position];
}

@Override
public int getCount() {

    return childFragments.length;
}

@Override
public CharSequence getPageTitle(int position){

    String title = getItem(position).getClass().getName();
    return title.subSequence(title.lastIndexOf(".") + 1, title.length());
}

Chat.java(片段)

public class Chat extends Fragment {

private TextView text1;

public void getUID(Bundle uid){
        String gotuid = uid.getString("uid");
        text1.setText(gotuid);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
    text1 = (TextView) rootView.findViewById(R.id.uidTextView);
    return rootView;
    }
}

我到处都放一些Log.i,发现Chat.java片段是在MainActivity.java的onStart()完成之后的某个时间创建的。如何显示/创建“片段”时让活动知道,以便活动可以向其发送数据?

任何帮助将不胜感激。

0 个答案:

没有答案