Ich möchte ein anderes Fragment in meinem Fragment anzeigen, das gezeigt wurde, und wenn ich es starte, wird es einen Fehler zeigen Fehler: (31, 75) error: kann Symbolmethode nicht finden findViewById (int), ist ihre Art, wie kann ich die findViewByID in einer Fragmentklasse nicht AppCompatActivity verwenden?Wie findViewById in einer öffentlichen Klasse verwendet wird ItemThreeFragment extends Fragment
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);
}
}
-Code für das Fragment, was im
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);
}
}
für 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);
}
}
Das ist meine Hauptaktivität
public class MainActivity erweitert AppCompatActivity Aufruf {
@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" />
, auf der Linie sind Sie diesen Fehler – YoLo
@YoLo diesem Teil Sir BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById (R.id.NavGroup); – Angelica
@Angelica Sie benötigen Ansicht Referenz zuerst Sie können so in OnCreateView() schreiben Methode Ansicht anzeigen = inflater.inflate (R.layout.fragment_group_two, Container, false); BottomNavigationView bottomNavigationView = (BottomNavigationView) view.findViewById (R.id.NavGroup); Rückansicht; können Sie jetzt schreiben, findViewById() –