清单片段未出现由于没有查看发现ID错误片段、清单、错误、发现

2023-09-03 21:43:20 作者:仗剑走天涯

我想推出一个列表的片段,但它只是似乎是工作在平板电脑上。当我在手机上运行我的应用程序的应用程序崩溃。有谁知道如何解决这个问题? code是如下。

错误

 致:java.lang.IllegalArgumentException:如果没有查看发现ID 0x7f0c0050(com.apptacularapps.exitsexpertlondonlite:ID / master_container)的片段FragmentMainList {b76424#1 ID = 0x7f0c0050}
 

MainActivity.java

 公共类MainActivity扩展ActionBarActivity {

    私人布尔mTwoPane;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        FragmentMainList newFragment =新FragmentMainList();
        FragmentTransaction交易= getSupportFragmentManager()的BeginTransaction()。
        transaction.replace(R.id.master_container,newFragment);
        器transaction.commit();

        如果(findViewById(R.id.detail_container)!= NULL){
            mTwoPane = TRUE;
        }
    }
}
 

activity_main.xml

 <片段
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / list_main
    机器人:名称=com.apptacularapps.exitsexpertlondonlite.FragmentMainList
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:FragmentMainList上下文=
    工具:布局=@安卓布局/ list_content/>
 
手机上的联系人名单只有姓名没有显示号码怎么办

activity_main.xml(sw600dp)

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向
    机器人:showDividers =中间
    工具:上下文=MainActivity。>

    < RelativeLayout的
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =1
        机器人:ID =@ + ID / master_container/>

    <的FrameLayout
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =3
        机器人:ID =@ + ID / detail_container/>

< / LinearLayout中>
 

FragmentMainList.java

 公共类FragmentMainList扩展android.support.v4.app.Fragment {

    ListView控件list_main;

    的String []的listContent = {
            项目1,
            项目2,
            第3项
    };

    私人布尔mTwoPane;

    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.fragment_main_list,集装箱,假);
        list_main =(ListView控件)v.findViewById(R.id.list_main);

        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(getActivity(),android.R.layout.simple_list_item_1,的listContent);
        list_main.setAdapter(适配器);

        返回伏;
    }
}
 

解决方案

那是因为你有这样的行

  transaction.replace(R.id.master_container,newFragment);
 

和你没有任何的内部布局 activity_main.xml 具有相同的ID为 master_container

I'm trying to launch a list fragment but it only seems to be working on tablets. When I run my app on phones the app crashes. Does anyone know how to resolve this issue? Code are below.

Error

Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0050 (com.apptacularapps.exitsexpertlondonlite:id/master_container) for fragment FragmentMainList{b76424 #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_width="match_parent"
    android:layout_height="match_parent"
    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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/master_container"/>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        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;
    }
}

解决方案

it's because you have this line

transaction.replace(R.id.master_container, newFragment);

and you don't have any layout inside activity_main.xml that has the same id as master_container