将AppCompactActivity中的布局元素设置为片段

时间:2018-07-21 14:36:16

标签: java android android-fragments appcompatactivity

一个AppCompactActivity类链接到3个片段,您可以在这些片段上浏览底部菜单。所有代码都在AppCompactActivity中。该应用程序在设置搜寻栏和textview时给我标记了错误[尝试在空对象引用上调用虚拟方法'void android.widget.SeekBar.setMax(int)']。 我不想在相应的Fragments的java文件中移动代码,是否可以平等地解决问题?

主要活动

public class OpSearchPlaceActivity extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener  {

    Button get_place, bSave;
    EditText etPosition;
    SeekBar sbDistance;
    TextView tvProgress,tvLat, tvLon;
    int progress = 2;
    int maxProgress = 80;


    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle toggle;
    public static final String LoginPREF = "login";
    SharedPreferences sharedpreferences;




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




        get_place = (Button) findViewById(R.id.getPlace);
        bSave = (Button) findViewById(R.id.savePlace);
        etPosition = (EditText) findViewById(R.id.position);
        sbDistance = (SeekBar) findViewById(R.id.sbDistance);
        tvProgress = (TextView) findViewById(R.id.tvProgress);
        tvLat = (TextView) findViewById(R.id.tvLat);
        tvLon = (TextView) findViewById(R.id.tvLon);
        sbDistance.setMax(maxProgress);

        sbDistance.setProgress(progress);
        tvProgress.setText("" + progress);

        sbDistance.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                progress = i;
                tvProgress.setText("" + progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

布局主要活动

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/dlMenu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ale.butastupa.OpSearchPlaceActivity">





    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_nav"></FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#c1c1c1"
        app:menu="@menu/bottom_menumap">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nvMenu"
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#ffffff"
        app:itemTextColor="@color/colorPrimaryDark"
        app:itemIconTint="@color/colorPrimaryDark"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">

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

</android.support.v4.widget.DrawerLayout>

片段1

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <EditText
                android:id="@+id/position"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="posizione attuale"
                android:layout_weight="2"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SELEZIONA PUNTO DIVERSO"
                android:id="@+id/getPlace"
                android:layout_weight="3"/>

        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"
            android:weightSum="5">

            <SeekBar
                android:id="@+id/sbDistance"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/tvProgress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="24dp"
                android:layout_weight="4"
                android:layout_centerHorizontal="true"
                android:layout_alignParentTop="true"/>



        </LinearLayout>

        <TextView
            android:id="@+id/tvLat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/tvLon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CERCA"
            android:id="@+id/savePlace"
            android:layout_gravity="center"/>

    </LinearLayout>

</RelativeLayout>

Java片段1

public class FilterOpSearchFragment extends Fragment  {






    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_opsearch_filter, container, false);




        return rootView;
    }


}

1 个答案:

答案 0 :(得分:1)

我解决了。只需使用“ getSystemService”函数,然后“膨胀”即可。通过这种方式

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoutFilter = inflater.inflate(R.layout.fragment_opsearch_filter, null);

    get_place = (Button) layoutFilter.findViewById(R.id.getPlace);
    bSave = (Button) layoutFilter.findViewById(R.id.savePlace);
    sbDistance = (SeekBar) layoutFilter.findViewById(R.id.sbDistance);
    tvProgress = (TextView) layoutFilter.findViewById(R.id.tvProgress);

但是现在bottom_menu覆盖了整个页面