No view found for id 0x7f0901d1.

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

java.lang.IllegalArgumentException: No view found for id 0x7f0901d1

问题

抱歉,您提供的文本中包含了许多代码和标记,因此无法提供一一翻译。如果您有特定的问题或需要关于代码的帮助,请提出具体的问题,我将尽力提供支持。

英文:

Hello everyone I am getting this error when trying to load a fragment when clicking a menu item to my application java.lang.IllegalArgumentException: No view found for id 0x7f0901d1 (com.example.testapp:id/signinfragment) for fragment Signin{79bb46d (c6057ec5-e830-4343-a89f-73175f885aa6) id=0x7f0901d1} I tried to load the fragment by using the id from the signin fragment I have created but the error still appears.

Main Activity.java

public class MainActivity extends AppCompatActivity implements  NavigationView.OnNavigationItemSelectedListener {

 NavigationView navigationView;

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


}




@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_login:
                getSupportFragmentManager().beginTransaction().replace(R.id.signinfragment,new Signin()).commit();
                break;
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }

}

Signin.java

public class Signin extends Fragment {


    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    public Signin() {
        // Required empty public constructor
    }



    public static Signin newInstance(String param1, String param2) {
        Signin fragment = new Signin();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_signin, container, false);
    }
}

fragment_signin.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/signinfragment"
    android:layout_height="match_parent"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Signin"
        android:orientation="horizontal" />

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/linlay_0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/app_background"
    tools:openDrawer="start">


    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"
        android:layout_gravity="start"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">

答案1

得分: 2

正确的用法是:

getSupportFragmentManager().beginTransaction().replace(R.id.id_of_view_to_be_replaced, new Signin()).commit();

因此,在您希望加载片段的活动中,您需要在布局中拥有一个要用片段替换的视图。

英文:

the correct usage is:

getSupportFragmentManager().beginTransaction().replace(R.id.id_of_view_to_be_replaced,new Signin()).commit();

So in your activity where you want to laod the fragment you need a view in your layout that you will replace with the fragment.

答案2

得分: 0

无法在您尝试用getSupportFragmentManager().beginTransaction().replace(R.id.signinfragment,new Signin()).commit();替换片段时,在您的活动布局中找到R.id.signinfragment的问题是因为您将此ID添加到片段布局中,而它应该附加到活动布局中的某个ViewGroup

英文:

> java.lang.IllegalArgumentException: No view found for id 0x7f0901d1 (com.example.testapp:id/signinfragment)

The problem that it can't find R.id.signinfragment in your activity layout when you are trying to replace a fragment with getSupportFragmentManager().beginTransaction().replace(R.id.signinfragment,new Signin()).commit();

this is because you add this id to the Fragment layout, while it should be attached to some ViewGroup in your activity layout instead

huangapple
  • 本文由 发表于 2020年8月10日 19:24:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63339238.html
匿名

发表评论

匿名网友

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

确定