具有多个片段的Activity中的FloatingActionButton?

时间:2017-01-23 22:41:31

标签: android android-fragments android-activity nullpointerexception floating-action-button

我有一个似乎无法解决的问题。

我有一个带有列表片段(横向和纵向)和addItem片段的活动。

Activity上有一个FloatingActionButton,但我似乎无法弄清楚如何设置它的onClickListener。

getViewById始终返回null,因此setOnClickListener没有要调用的对象。为什么这样,我该如何解决?

我做错了多个片段,这是我的问题吗?

注意:为了简洁起见,我遗漏了景观片段/布局。它与具有不同身份的肖像相同。

MainActivity.java

package tlw.app;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> activeFragments = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setContentView(R.layout.fragment_list);

        FragmentManager fManager = getFragmentManager();
        FragmentTransaction fTrans = fManager.beginTransaction();
        Configuration configInfo = getResources().getConfiguration();

        activeFragments.clear();
        if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
            ListFragmentLand fragmentListLand = new ListFragmentLand();
            fTrans.replace(R.id.main_container, fragmentListLand);

            activeFragments.add("list_land");
        } else {
            ListFragment fragmentListPort = new ListFragment();
            fTrans.replace(R.id.main_container, fragmentListPort);
            activeFragments.add("list_port");
        }
        fTrans.commit();

        if (activeFragments.contains("list_port") || activeFragments.contains("list_land")){
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    FragmentManager fManager = getFragmentManager();
                    FragmentTransaction fTrans = fManager.beginTransaction();

                    activeFragments.clear();
                    ItemAddFragment fragmentAdd = new ItemAddFragment();
                    fTrans.replace(R.id.main_container, fragmentAdd);
                    activeFragments.add("add");
                    fTrans.commit();
                }
            });
        }
    }
}

activity_main.xml中          

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />


    <FrameLayout
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</android.support.design.widget.CoordinatorLayout>

ListFragment.xml

package tlw.app;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ListFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list, container, false);
    }
}

fragment_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="tlw.app.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="List Portrait"/>

</LinearLayout>

ItemAddFragment.java

package tlw.app;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ItemAddFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add, container, false);
    }
}

fragment_add.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_add"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name:"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText_name"
            />
    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

请更改MainActivity

中此部分代码

发件人

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setContentView(R.layout.fragment_list);

 @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

错误::

- 传递错误的xml实例::

- 在实际设置xml之前尝试获取工具栏..

请告诉我们是否需要更多帮助

答案 1 :(得分:1)

setContentView(R.layout.fragment_list);

这将设置用于您没有FAB的活动的布局。将此行更改为

setContentView(R.layout.activity_main);

通常,每当findViewById()返回null时,您需要确保加载了正确的XML并且它包含您要查找的id