Android onCreateView“;称自己为;
我试图用2个片段、一行按钮和一个列表片段启动一个应用程序,但当我启动它时,会调用应用程序的onCreateView,调用GetLayoutFlator以获取布局充气器。这反过来调用inflate,并将其传递给根布局文件的名称。在这段代码中,再次调用onCreateView,导致无限递归。这是密码Android onCreateView“;称自己为;,android,Android,我试图用2个片段、一行按钮和一个列表片段启动一个应用程序,但当我启动它时,会调用应用程序的onCreateView,调用GetLayoutFlator以获取布局充气器。这反过来调用inflate,并将其传递给根布局文件的名称。在这段代码中,再次调用onCreateView,导致无限递归。这是密码 @Override public View onCreateView(String name, Context context, AttributeSet attrSet) { Log.i(T
@Override
public View onCreateView(String name, Context context, AttributeSet attrSet) {
Log.i(TAG, "onCreateView, name = " + name + " context " + context + " AttributeSet = " + attrSet);
View v = null; //super.onCreateView(name, context, attrSet);
try {
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.activity_shop2_drop,null);
Log.i(TAG,"passed to LayoutInflater: " + R.layout.activity_shop2_drop);
}
catch(Exception e) {
Log.e(TAG, "onCreateView failed: " + e.getMessage());
}
return v;
}
这里的问题是,我调用Fragment.onCreateView传递包含视图的id。这个包含视图就是视图本身,所以代码自然会试图将我的代码附加到它认为是包含视图的内容上,即它本身。因此是无限递归。另一个问题是,膨胀代码在activity_shop2_drop.xml中看到“fragment”标记时返回ClassNotFoundException,因此从不调用片段上的“onCreateView”。我还没有解决这个问题。以下是activity_shop2_drop.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/whole_app_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
class="com.victorpreston.shop2drop.S2DButtonFragment"
android:id="@+id/buttonFragmetLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
class="com.victorpreston.shop2drop.S2DListFragment"
android:id="@+id/listFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonFragmentLayout"
android:layout_width="match_parent"
android:layout_height="40dip"
android:orientation="horizontal">
<Button
android:id="@+id/newItemButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/newColumnButton" />
<Button
android:id="@+id/newColumnButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/newItemButton" />
<Button
android:id="@+id/exitButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/exitButton" />
</LinearLayout>
下面是Button_fragment_layout.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/whole_app_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
class="com.victorpreston.shop2drop.S2DButtonFragment"
android:id="@+id/buttonFragmetLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
class="com.victorpreston.shop2drop.S2DListFragment"
android:id="@+id/listFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonFragmentLayout"
android:layout_width="match_parent"
android:layout_height="40dip"
android:orientation="horizontal">
<Button
android:id="@+id/newItemButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/newColumnButton" />
<Button
android:id="@+id/newColumnButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/newItemButton" />
<Button
android:id="@+id/exitButton"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="1"
android:text="@string/exitButton" />
</LinearLayout>
请注意,它们都创建并使用相同的id。我尝试更改它,但没有任何区别,因此我认为这不是问题所在。能否显示您的
activity\u shop2\u drop
文件?我想您希望覆盖Fragment.onCreateView
而不是activity.onCreateView
我尝试显示我的xml文件,但由于某种原因,StackOverflow工具不允许我使用。在任何情况下,我想我已经明白了这一点:onCreateView确实为子布局调用了自己。我只是没有足够的耐心,没有足够多的时间来遍历它,返回到根视图,从而找到函数的结尾。我是一个试图孜孜不倦地跟踪正在发生的事情的受害者。@vmironov确实有它的基本正确性;onCreateView是我需要放置创建片段布局的代码的地方。但是,现在我有一个不同的问题:它永远无法访问Fragment.onCreateView,因为主布局在遇到“Fragment”标记时失败。我可以通过不使用“fragment”标签来解决这个问题,但每个人都这样做,所以这一定是可能的。“fragment”标记是在哪里定义的,这样我就可以确保我的库中有它了?