【问题标题】:Error when I Tried to show or hide a Floating Action Button from a fragment当我尝试在片段中显示或隐藏浮动操作按钮时出错
【发布时间】:2021-06-16 22:53:00
【问题描述】:

我试图从一个片段移动到另一个片段,隐藏浮动操作按钮,然后从第二个片段返回到第一个片段并再次显示按钮。浮动操作按钮位于主 Activity 内。

Mainactivity.java:

package cl.itn.cam;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.fragment.NavHostFragment;

import android.view.View;

public class NCR extends AppCompatActivity {

    FragmentTransaction transaction;
    Fragment fragmentFirst, fragmentSecond;
    FloatingActionButton fab = findViewById(R.id.fab);

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

        fragmentFirst = new FirstFragment();
        fragmentSecond = new SecondFragment();


    }

    public FloatingActionButton getFloatingActionButton() {
        return fab;

    }

    public void showFloatingActionButton() {
        fab.show();
    }

    public void hideFloatingActionButton() {
        fab.hide();
    }

}

FirstFragment.java:

public class FirstFragment extends Fragment {

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


        final FloatingActionButton fab = ((NCR) getActivity()).getFloatingActionButton();

        if (fab != null) {
            ((NCR) getActivity()).hideFloatingActionButton();
        }

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });
    }
}

SecondFragment.java:

public class SecondFragment extends Fragment {

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


        final FloatingActionButton fab = ((NCR) getActivity()).getFloatingActionButton();

        if (fab != null) {
            ((NCR) getActivity()).showFloatingActionButton();
        }



        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(SecondFragment.this)
                        .navigate(R.id.action_SecondFragment_to_FirstFragment);
            }
        });
    }

应用程序在尝试使用以下堆栈跟踪显示主要活动时崩溃:

** 2021-03-19 11:04:56.979 32471-32471/cl.itn.cam E/AndroidRuntime: 致命异常: main 进程:cl.itn.cam,PID:32471 java.lang.RuntimeException:无法实例化活动 ComponentInfo{cl.itn.cam/cl.itn.cam.NCR}:java.lang.NullPointerException:尝试调用虚拟方法 'android.content.pm.ApplicationInfo android.content。 Context.getApplicationInfo()' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3401) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3620) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2183) 在 android.os.Handler.dispatchMessage(Handler.java:107) 在 android.os.Looper.loop(Looper.java:241) 在 android.app.ActivityThread.main(ActivityThread.java:7604) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' **

【问题讨论】:

    标签: java android fragment floating-action-button


    【解决方案1】:

    解决了!

    在公共课 NCR,我替换了“FloatingActionButton fab = findViewById(R.id.fab);”通过“FloatingActionButton fab;”,然后在 onCreate 事件中我添加了“fab = findViewById(R.id.fab);”

    谢谢!

    【讨论】:

      【解决方案2】:

      尝试替换

      final FloatingActionButton fab = ((NCR) getActivity()).getFloatingActionButton();

      FloatingActionButton fab = findViewById(R.id.fab);
      

      在您的片段中。这应该可以消除错误

      【讨论】:

        猜你喜欢
        • 2017-08-12
        • 2016-09-29
        • 2016-07-27
        • 1970-01-01
        • 2015-12-13
        • 2020-05-28
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        相关资源
        最近更新 更多