英文:
No view found for id 0x7f0800c2 (.... id/profile_frame) for fragment ProfileFragment{763e0e0 #2 id=0x7f0800c2}
问题
I've translated the code portion for you:
按钮代码:
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImagesActivity();
}
});
从一个片段切换到另一个片段的代码:
private void openImagesActivity() {
ProfileFragment profileFragment = new ProfileFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.profile_frame, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
第二个片段的布局:
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileFragment"
android:id="@+id/profile_frame"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/changeProfilePicBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change profile picture"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />
<ImageView
android:id="@+id/profilePicView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/changeProfilePicBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="409dp"
android:layout_height="467dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
活动布局代码:
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="@+id/mainView"
tools:visibility="visible"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/nav_item_colors"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_above="@id/main_nav"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
活动代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
weatherFragment = new WeatherFragment();
uploadFragment = new UploadFragment();
searchFragment = new SearchFragment();
profileFragment = new ProfileFragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home :
setFragment(homeFragment);
return true;
case R.id.nav_weather :
setFragment(weatherFragment);
return true;
case R.id.nav_upload :
setFragment(uploadFragment);
return true;
case R.id.nav_search :
setFragment(searchFragment);
return true;
case R.id.nav_profile :
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
if (!(FirebaseAuth.getInstance().getCurrentUser() != null)) {
Intent i = new Intent (HomeActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}
Please note that the translation might include certain XML escape sequences, such as ", which represent double quotes ("). You may need to replace them with actual double quotes if you encounter any issues with the code.
英文:
I've already taken a look at some of the other questions regarding this error but I've either not fully understood them, or they don't work for my solution, so I would like to ask it here.
I'm getting the error mentioned in the title when trying to press a button to go from one fragment to another one.
Button code:
mTextViewShowUploads.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openImagesActivity();
}
});
Go from one fragment to another code:
private void openImagesActivity() {
ProfileFragment profileFragment = new ProfileFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.profile_frame, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Layout of second fragment
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileFragment"
android:id="@+id/profile_frame"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/changeProfilePicBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change profile picture"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.26999998" />
<ImageView
android:id="@+id/profilePicView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/changeProfilePicBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="409dp"
android:layout_height="467dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
Activity layout code
<RelativeLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:id="@+id/mainView"
tools:visibility="visible"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/nav_item_colors"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/nav_items">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_above="@id/main_nav"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
weatherFragment = new WeatherFragment();
uploadFragment = new UploadFragment();
searchFragment = new SearchFragment();
profileFragment = new ProfileFragment();
setFragment(homeFragment);
mMainNav.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home :
setFragment(homeFragment);
return true;
case R.id.nav_weather :
setFragment(weatherFragment);
return true;
case R.id.nav_upload :
setFragment(uploadFragment);
return true;
case R.id.nav_search :
setFragment(searchFragment);
return true;
case R.id.nav_profile :
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
@Override
protected void onStart() {
super.onStart();
if(!(FirebaseAuth.getInstance().getCurrentUser() != null)) {
Intent i = new Intent (HomeActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}
}
答案1
得分: 2
你正在尝试将 R.id.profile_frame 作为要替换片段的容器,而 profile_frame 是片段布局中容器布局的 ID。你想要在这一行中传递活动的容器布局 ID:
fragmentTransaction.replace(R.id.profile_frame, profileFragment);
将该行更改为:
fragmentTransaction.replace(R.id.main_frame, profileFragment);
我可以看到 main_frame 是你想要显示片段的容器的 ID。
英文:
You're trying to pass R.id.profile_frame as the container in which to replace the fragment, and profile_frame is the id of the container layout in your fragment's layout. You want to pass the container layout id of the activity in this line:
fragmentTransaction.replace(R.id.profile_frame, profileFragment);
Change that line to:
fragmentTransaction.replace(R.id.main_frame, profileFragment);
As I can see main_frame is the id of the container you wanna show your fragment in.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论