更改组件可见性时 Android Studio 空对象引用错误

Posted

技术标签:

【中文标题】更改组件可见性时 Android Studio 空对象引用错误【英文标题】:Android studio null object reference error while changing component visibility 【发布时间】:2020-12-08 05:30:41 【问题描述】:

我正在制作一个应用程序,它为不同的用户显示相同的菜单,每个菜单都是一个片段,但根据用户的不同,某些东西是可见的,而另一些则不可见。

但是,当我尝试将组件的可见性设置为 GONE 时,我得到一个空指针异常错误,我仍在测试,所以我只在片段 xml 上使用 textView。

我已经尝试在 onCreate() 方法之前的 if 子句中和 setContentView() 之后初始化 TextViewFragmentMenuEncomenda,我正在使用的片段的类,对象,例如 tabLayoutviewPager

if 子句只是为了确保用户类型正确,有问题的代码如下:

public class PaginaInicialCliente extends AppCompatActivity 

    private SharedPreferences sharedpreferences;

    private TabLayout tabLayout;
    private ViewPager viewPager;

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

        tabLayout = findViewById(R.id.tabLayoutCliente);
        viewPager = findViewById(R.id.viewPagerCliente);
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        //fragments a adicionar
        adapter.addFragment(new FragmentMenuEncomendas(), "Encomendas");
        adapter.addFragment(new FragmentMenuOpcoes(), "Opções");

        //inicializar o tab layout
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);

        if (sharedpreferences.getInt("tipo",0)==3) 
            Log.d("USER_DEBUG","Cliente");
            FragmentMenuEncomendas fragmentMenuEncomendas = new FragmentMenuEncomendas();
            
            //THE ERROR IS HERE
            fragmentMenuEncomendas.getActivity().findViewById(R.id.textViewEncomendas).setVisibility(View.GONE);
        

    

FragmentMenuEncomenda 类:

public class FragmentMenuEncomendas extends Fragment 

    View v;

    public FragmentMenuEncomendas()
    

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        v = inflater.inflate(R.layout.encomendas_fragment, container, false);
        return v;
    

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_
    android:layout_>

    <TextView
        android:id="@+id/textViewEncomendas"
        android:layout_
        android:layout_
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我搜索的所有答案都不起作用,我不知道是不是因为我试图改变 on create 方法中的可见性。

【问题讨论】:

【参考方案1】:

因为调用函数时没有创建视图。

public class FragmentMenuEmpresas extends Fragment 

    View v;
    View textView;
    boolean isTextViewShow = false;
    boolean isViewCreated = false;

    public FragmentMenuEmpresas() 
    

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        v = inflater.inflate(R.layout.empresas_fragment, container, false);
        textView = v.findViewById(R.id.textViewEncomendas);
        return v;
    

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

        if (isTextViewShow) 

            textView.setVisibility(View.VISIBLE);
         else 

            textView.setVisibility(View.GONE);
        

        isViewCreated = true;
    

    void setIsTextViewShow() 

        if (isViewCreated) 

            textView.setVisibility(View.VISIBLE);
         else 

            isTextViewShow = true;
        
    

    void setIsTextViewGone() 
        
        if (isViewCreated) 

            textView.setVisibility(View.GONE);
         else 

            isTextViewShow = false;
        
    

那么您可以随时拨打setIsTextViewShow setIsTextViewGone

 FragmentMenuEncomendas fragmentMenuEncomendas = new FragmentMenuEncomendas();
                       
 fragmentMenuEncomendas.setIsTextViewShow();//show
 fragmentMenuEncomendas.setIsTextViewGone();//gone

【讨论】:

我如何将可见性更改为消失? 它可以工作,非常感谢,但现在当我以另一个用户类型登录时,它仍然消失,即使我调用了 setIsTextViewShow()

以上是关于更改组件可见性时 Android Studio 空对象引用错误的主要内容,如果未能解决你的问题,请参考以下文章

当我调用可见性时,Android 动画不起作用

如何处理自定义 android 视图/小部件的可见性更改

更改 android studio 中的 textview 可见性

Android动画问题——可见性变化比动画快

ListView 使 textview 在超出屏幕可见性时可见

折叠可见性时如何禁用对组合框元素的验证?