java - 将 AppCompactActivity 中的布局元素设置为 fragment

标签 java android android-fragments appcompatactivity

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

主要 Activity

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) {

            }
        });

布局主要 Activity

<?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>

fragment 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 fragment 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;
    }


}

最佳答案

我解决了。只需使用“getSystemService”函数,然后“inflate”即可。这样

    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覆盖了整个页面

关于java - 将 AppCompactActivity 中的布局元素设置为 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51457037/

相关文章:

java - 如何以编程方式将标题栏颜色更改为十六进制值?

Android 导航 popBackStack

android - 选项卡式 fragment 应用程序应该只有一个 Activity ?

java - 选择要使用哪个 fragment 的 Android (Java) Activity

java - Jersey Web 服务不处理 XML,而是处理 JSON

java - DBUnit 忽略 xml 元素

android - 构建应用程序时出现警告 : "Expected stack map table for method with non-linear control flow."

java - 在 TextView 中保留最近收到的短信

java - Persistence.xml 中的意外元素 Entitymanager 注入(inject)不起作用

android - 具有多个 Android 客户端的服务器