No view found for id 0x7f0800c2 (…. id/profile_frame) for fragment ProfileFragment{763e0e0 #2 id=0x7f0800c2}

huangapple go评论141阅读模式
英文:

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 &quot;, 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

&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.ProfileFragment&quot;
android:id=&quot;@+id/profile_frame&quot;
&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;Button
android:id=&quot;@+id/changeProfilePicBtn&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Change profile picture&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;0.26999998&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/profilePicView&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/changeProfilePicBtn&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
tools:src=&quot;@tools:sample/avatars&quot; /&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recycler_view&quot;
android:layout_width=&quot;409dp&quot;
android:layout_height=&quot;467dp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_constraintVertical_bias=&quot;1.0&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/FrameLayout&gt;

Activity layout code

&lt;RelativeLayout 
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:visibility=&quot;visible&quot;
android:id=&quot;@+id/mainView&quot;
tools:visibility=&quot;visible&quot;
&gt;
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/main_nav&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;56dp&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_alignParentBottom=&quot;true&quot;
app:itemBackground=&quot;@color/colorPrimary&quot;
app:itemIconTint=&quot;@color/nav_item_colors&quot;
app:labelVisibilityMode=&quot;unlabeled&quot;
app:menu=&quot;@menu/nav_items&quot;&gt; 
&lt;/com.google.android.material.bottomnavigation.BottomNavigationView&gt;
&lt;FrameLayout
android:id=&quot;@+id/main_frame&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_above=&quot;@id/main_nav&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;/FrameLayout&gt;
&lt;/RelativeLayout&gt;

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.

huangapple
  • 本文由 发表于 2020年1月6日 21:47:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定