由于未找到 ID 错误的视图,列表片段未出现
Posted
技术标签:
【中文标题】由于未找到 ID 错误的视图,列表片段未出现【英文标题】:List fragment not appearing owing to No view found for ID error 【发布时间】:2015-08-18 06:36:30 【问题描述】:我正在尝试启动一个列表片段,但它似乎只适用于平板电脑。当我在手机上运行我的应用程序时,应用程序崩溃了。有谁知道如何解决这个问题?代码如下。
错误
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 (com.apptacularapps.exitsexpertlondonlite:id/master_container) for fragment FragmentMainListb76424 #1 id=0x7f0c0050
MainActivity.java
public class MainActivity extends ActionBarActivity
private boolean mTwoPane;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentMainList newFragment = new FragmentMainList();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
if (findViewById(R.id.detail_container) != null)
mTwoPane = true;
activity_main.xml
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_main"
android:name="com.apptacularapps.exitsexpertlondonlite.FragmentMainList"
android:layout_
android:layout_
tools:context=".FragmentMainList"
tools:layout="@android:layout/list_content"/>
activity_main.xml (sw600dp)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_
android:layout_
android:layout_weight="1"
android:id="@+id/master_container"/>
<FrameLayout
android:layout_
android:layout_
android:layout_weight="3"
android:id="@+id/detail_container"/>
</LinearLayout>
FragmentMainList.java
public class FragmentMainList extends android.support.v4.app.Fragment
ListView list_main;
String[] listContent =
"Item 1",
"Item 2",
"Item 3"
;
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_main_list, container, false);
list_main = (ListView)v.findViewById(R.id.list_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,listContent);
list_main.setAdapter(adapter);
return v;
【问题讨论】:
【参考方案1】:因为你有这条线
transaction.replace(R.id.master_container, newFragment);
并且您在 activity_main.xml
中没有任何布局与 master_container
具有相同的 id
【讨论】:
以上是关于由于未找到 ID 错误的视图,列表片段未出现的主要内容,如果未能解决你的问题,请参考以下文章
IllegalArgumentException:尝试 setView 时未找到片段 id 的视图