java.lang.NullPointerException:空对象引用上的 FloatingActionButton.setVisibility(int)'

Posted

技术标签:

【中文标题】java.lang.NullPointerException:空对象引用上的 FloatingActionButton.setVisibility(int)\'【英文标题】:java.lang.NullPointerException: FloatingActionButton.setVisibility(int)' on a null object referencejava.lang.NullPointerException:空对象引用上的 FloatingActionButton.setVisibility(int)' 【发布时间】:2020-03-31 06:37:07 【问题描述】:

我创建了一个 Activity,其中包含四个浮动按钮和一个片段,该片段调用 Activity 的浮动按钮以使它们可见/不可见,但是当我从片段访问这些浮动按钮时,它会引发空指针异常。

活动代码:

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

    getInitialized();

    fabSettings = (FloatingActionButton) this.findViewById(R.id.fabSetting);
    fabStory = (FloatingActionButton) this.findViewById(R.id.fabStory);
    fabImage = (FloatingActionButton) this.findViewById(R.id.fabImage);
    fabVideo = (FloatingActionButton) this.findViewById(R.id.fabVideo);


片段代码:

 @SuppressLint("RestrictedApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 

    View v = inflater.inflate(R.layout.fragment_all_community, container, false);


    SessionManager sessionManager = new SessionManager(getActivity());
    if(sessionManager.isLoggedIn())


        ActivityUser.fabSettings.setVisibility(View.VISIBLE);
        ActivityUser.laySearchCode.setVisibility(View.VISIBLE);
        ActivityUser.bottomLayout.setVisibility(View.VISIBLE);

        tvCreateCommunity.setVisibility(View.VISIBLE);
    else
        MainActivity.fabSettings.setVisibility(View.VISIBLE);
        MainActivity.bottomLayout.setVisibility(View.VISIBLE);
        MainActivity.laySearchCode.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).getSupportActionBar().setTitle("");
        tvCreateCommunity.setVisibility(View.GONE);
    
 

请帮忙

【问题讨论】:

不要在Activity中使用静态视图变量..也不要直接从片段访问Activity的视图..改为创建一个方法.. 为此使用接口 【参考方案1】:

使用此代码

片段:

public class Fragment extends Fragment 

    public interface onHideEventListener 
        public void hideEvent(boolean isVisible);
    

    onHideEventListener hideEventListener;

    @Override
    public void onAttach(Activity activity) 
        super.onAttach(activity);
        try 
            hideEventListener = (onHideEventListener) activity;
         catch (ClassCastException e) 
            throw new ClassCastException(activity.toString() + " must implement onHideEventListener");
        
    

    final String LOG_TAG = "myLogs";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) 
        View v = inflater.inflate(R.layout.fragment2, null);

        SessionManager sessionManager = new SessionManager(getActivity());
        if(sessionManager.isLoggedIn())
            hideEventListener.hideEvent(false);
        else
            hideEventListener.hideEvent(true);
        
        return v;
    

活动:

public class MainActivity extends Activity implements onSomeEventListener

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    

    @Override
    public void hideEvent(boolean isVisible) 
        fabSettings.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
        laySearchCode.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
        bottomLayout.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
    

【讨论】:

以上是关于java.lang.NullPointerException:空对象引用上的 FloatingActionButton.setVisibility(int)'的主要内容,如果未能解决你的问题,请参考以下文章

亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce

来自 LayerDrawable 的异常

Microsoft Access 和 Java JDBC-ODBC 错误

如何使用其id获取单个SQLite行数据?