Android 找不到ID
我创建了一个包含ID为“container”的FrameLayout的活动,虽然我无法引用此视图,但我已在一个简单的引用中对其进行了测试: 活动布局:Android 找不到ID,android,layout,Android,Layout,我创建了一个包含ID为“container”的FrameLayout的活动,虽然我无法引用此视图,但我已在一个简单的引用中对其进行了测试: 活动布局: <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"
<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"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="app.chrono.name.LoginActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</FrameLayout>
我不明白为什么这个容器不能被引用。它应该是可访问的。它以红色书写,似乎不存在。容器是一个视图,但具体而言,它是一个布局视图,即FrameLayout 因此,将类型更改为FrameLayout而不是View
FrameLayout container = (FrameLayout)findViewById(R.id.container);
编辑:
另外,如果您发现第一个解决方案不起作用,请直接在片段转换中添加id,如下所示
fragmentTransaction.add(R.id.container, new LoginFragment());
你的代码是对的,
点击代码findviewbyid下面的一行,一个建议是按alt+enter,仅此而已。
同时尝试使缓存无效并重新启动:
转到文件并使缓存无效并重新启动。您应该commit()代码>您的片段事务
FragmentManager fragmentManager = activity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
fragmentTransaction.commit();
首先从代码中删除这一行
container = findViewById(R.id.container);
使用此函数打开片段
public void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(current);
fragmentTransaction.add(R.id.container, new LoginFragment());
}
然后像这样再打电话给我
loadFragment(new LoginFragment());
确保你能导入这些
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
app.chrono.-.LoginActivity这就是问题所在吗?tools:context=“app.chrono.-.LoginActivity”>
为什么会有一个“-”字母?另外,有3个FrameLayout正在打开,但有2个正在关闭。首先解决它们。按实际情况发布活动布局。你在问题里贴了什么毫无意义。还有什么我不能参考这个视图
的真正含义?我只是删除了应用程序名,这一点都不重要。第二,为什么需要整个活动,只有课堂围绕着它,没有其他。“容器”视图/框架布局似乎根本不存在。这就是问题所在。它找不到容器本身。它是用红色写的。当我尝试alt输入“container”时,它告诉我创建一个新的值资源。这并没有给出解决方案。问题在于容器。它似乎不存在,用红色书写。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;