Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在公共类ItemThreeFragment扩展片段中使用findViewById_Android_Android Fragments - Fatal编程技术网

Android 如何在公共类ItemThreeFragment扩展片段中使用findViewById

Android 如何在公共类ItemThreeFragment扩展片段中使用findViewById,android,android-fragments,Android,Android Fragments,我想在已显示的片段中显示另一个片段,当我运行它时,它将显示一个错误错误:(31,75)错误:找不到符号方法findViewById(int),它们是我可以在片段类中使用findViewById的某种方式吗 ItemThreeFragment.java public class ItemThreeFragment extends Fragment { private BottomNavigationView bottomNavigationView; public static ItemT

我想在已显示的片段中显示另一个片段,当我运行它时,它将显示一个错误错误:(31,75)错误:找不到符号方法findViewById(int),它们是我可以在片段类中使用findViewById的某种方式吗

ItemThreeFragment.java

public class ItemThreeFragment extends Fragment {


private BottomNavigationView bottomNavigationView;

public static ItemThreeFragment newInstance() {
    ItemThreeFragment fragment = new ItemThreeFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.NavGroup);

    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener(){

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment1 = null;
                    switch (item.getItemId()){
                        case R.id.groupJoined:
                            selectedFragment1 = GroupOne.newInstance();
                            break;
                        case R.id.createGroup:
                            selectedFragment1 = GroupTwo.newInstance();
                            break;
                    }

                    FragmentTransaction trans = getChildFragmentManager().beginTransaction();
                    trans.replace(R.id.groupFrame_layout, selectedFragment1);
                    trans.commit();
                    return true;

                }
            });


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_item_three, container, false);

}
}
我调用的片段的代码

GroupOne.java

public class GroupOne extends Fragment {
public static GroupOne newInstance() {
    GroupOne fragment = new GroupOne();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_group_one, container, false);
}
}
对于GroupTwo.java

public class GroupTwo extends Fragment {
public static GroupTwo newInstance() {
    GroupTwo fragment = new GroupTwo();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_group_two, container, false);
}
}
这是我的主要活动

公共类MainActivity扩展了AppCompatActivity{

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

    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.NavBot);

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;
                    switch (item.getItemId()) {
                        case R.id.home:
                            selectedFragment = ItemOneFragment.newInstance();
                            break;
                        case R.id.search:
                            selectedFragment = ItemTwoFragment.newInstance();
                            break;
                        case R.id.groups:
                            selectedFragment = ItemThreeFragment.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, ItemOneFragment.newInstance());
    transaction.commit();

    //Used to select an item programmatically
    //bottomNavigationView.getMenu().getItem(2).setChecked(true);
}
activity_main.xml

<android.support.design.widget.CoordinatorLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.MainActivity">

<FrameLayout
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigation"
    android:animateLayoutChanges="true"
    android:layout_below="@+id/bottomNavigationView">
</FrameLayout>

<android.support.design.widget.BottomNavigationView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/NavBot"
    android:layout_gravity="bottom"
    app:menu="@menu/menu_nav"
    android:background="@color/Lime"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"></android.support.design.widget.BottomNavigationView>

fragment_item_three.xml

<ScrollView 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/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:weightSum="1">

    <SearchView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/searchGroup"
        android:scrollbarSize="10dp"/>

    <android.support.design.widget.BottomNavigationView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/NavGroup"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_group"
        android:background="@color/Lime"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"></android.support.design.widget.BottomNavigationView>

    <FrameLayout
        android:id="@+id/groupFrame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true"
        android:layout_below="@+id/bottomNavigationView">
    </FrameLayout>

</LinearLayout>

fragment_group_one.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.GroupOne">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:text="Hay salamat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3" />

fragment_group_two.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.findforme.www.myapplication.GroupTwo">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hay Humana" />

示例代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_group_two, container, false);
    TextView tv = (TextView) view.findViewById(R.id.tv)
    return view;
}
示例代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_group_two, container, false);
    TextView tv = (TextView) view.findViewById(R.id.tv)
    return view;
}

这就是我问题的答案

 private void findViews(View view)
{
    bottomNavigationView = (BottomNavigationView)view.findViewById(R.id.NavGroup);
    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener(){

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment1 = null;
                    switch (item.getItemId()){
                        case R.id.groupJoined:
                            selectedFragment1 = GroupOne.newInstance();
                            break;
                        case R.id.createGroup:
                            selectedFragment1 = GroupTwo.newInstance();
                            break;
                    }

                    FragmentTransaction trans = getFragmentManager().beginTransaction();
                    trans.replace(R.id.groupFrame_layout, selectedFragment1);
                    trans.commit();
                    return true;

                }
            });
}
然后添加onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_three, container, false);
    findViews(view);
    return view;

}

这就是我问题的答案

 private void findViews(View view)
{
    bottomNavigationView = (BottomNavigationView)view.findViewById(R.id.NavGroup);
    bottomNavigationView.setOnNavigationItemSelectedListener(
            new BottomNavigationView.OnNavigationItemSelectedListener(){

                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment1 = null;
                    switch (item.getItemId()){
                        case R.id.groupJoined:
                            selectedFragment1 = GroupOne.newInstance();
                            break;
                        case R.id.createGroup:
                            selectedFragment1 = GroupTwo.newInstance();
                            break;
                    }

                    FragmentTransaction trans = getFragmentManager().beginTransaction();
                    trans.replace(R.id.groupFrame_layout, selectedFragment1);
                    trans.commit();
                    return true;

                }
            });
}
然后添加onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_three, container, false);
    findViews(view);
    return view;

}

你在哪一条线上得到这个error@YoLo这部分sir BottomNavigationView BottomNavigationView=(BottomNavigationView)findViewById(R.id.NavGroup);@Angelica您需要先获取视图引用,您可以在onCreateView()方法view view=inflater.inflate(R.layout.fragment\u group\u two,container,false)中这样写;BottomNavigationView BottomNavigationView=(BottomNavigationView)view.findViewById(R.id.NavGroup);返回视图;现在可以编写视图findViewById()@VíVèkBårè236;它将停止应用程序sir@Angelica我可以看看你的布局xml吗?你在哪一行得到这个error@YoLo这部分sir BottomNavigationView BottomNavigationView=(BottomNavigationView)findViewById(R.id.NavGroup);@Angelica您首先需要获取视图引用,您可以在onCreateView()中这样编写方法视图=充气器.inflate(R.layout.fragment\u group\u two,container,false);BottomNavigationView底部导航视图=(BottomNavigationView)视图。findViewById(R.id.NavGroup);返回视图;现在可以编写视图findViewById()@VíVèkBårè236;它将停止应用程序sir@Angelica我可以看看你的布局xml吗?是的,这个正确的答案是的,这个正确的答案非常感谢,这是一个非常简单和有用的答案:)非常感谢,这是一个非常简单和有用的答案:)