未找到ID为片段视图 [英] No view found for id for fragment

查看:239
本文介绍了未找到ID为片段视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经被困在这一切的一天,我似乎无法找到这里的答案适合我的情况非常接近,我要投入使用。

下面就一起来看看我的UI,标签和列表视图功能很好:的http://我。 stack.imgur.com/84kRY.png

当我点击画像列表项,喜欢蓝色,我希望它显示一个新片段是一个蓝色的屏幕,景观它会显示片段一半在屏幕上,并在其他列表视图。我使用ActionBarSherlock和片段做到这一点。

我建立它由内而外的,所以我开始用一个单独的列表,并担任我想它。然后,我添加的标签,寻呼机的功能,以及其他列表。某处在这个过程片段上展示停止工作,每次我点击任何东西我得到一个崩溃。

这里是我的ListView活动之一的clicky部分

 公共类TitlesFragment扩展SherlockListFragment {布尔mDualPane;
INT mCurCheckPosition = 0;    。
    。
    。@覆盖
公共无效的onSaveInstanceState(捆绑outState){
    super.onSaveInstanceState(outState);
    outState.putInt(curChoice,mCurCheckPosition);
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    showDetails(位置);
}无效showDetails(INT位置){
    SherlockFragment newContent =新SherlockFragment();
    FragmentManager FM = getFragmentManager();
    FragmentTransaction英尺= fm.beginTransaction();
    如果(mDualPane){
        开关(位置){
        情况下0:
            newContent =新蓝();
            打破;
        情况1:
            newContent =新红();
            打破;
        案例2:
            newContent =新绿();
            打破;
        案例3:
            newContent =新黄();
            打破;
        情况4:
            newContent =新紫();
            打破;
    } ft.replace(R.id.details,newContent);
    }其他{
        开关(位置){
        情况下0:
            newContent =新蓝();
            打破;
        情况1:
            newContent =新红();
            打破;
        案例2:
            newContent =新绿();
            打破;
        案例3:
            newContent =新黄();
            打破;
        情况4:
            newContent =新紫();
            打破;
    } ft.replace(R.id.titles,newContent);
    }
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(NULL);
    ft.commit();
    返回;
}
}

这里的蓝色类

 公共类蓝扩展SherlockFragment {@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
}公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    返回inflater.inflate(R.layout.blue,集装箱,FALSE);
}}

蓝布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID /蓝
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#0000FF
机器人:方向=垂直>< / LinearLayout中>

我希望它在横向替换布局

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:baselineAligned =假
机器人:方向=横向
机器人:ID =@ + ID / fragment_layout_support_land><片段
    机器人:ID =@ + ID / titles_land
    机器人:layout_width =0像素
    机器人:layout_height =match_parent
    机器人:layout_weight =1
    类=*** TitlesFragment/><片段
    机器人:ID =@ + ID /详细信息
    机器人:layout_width =0像素
    机器人:layout_height =match_parent
    机器人:layout_weight =2/>< / LinearLayout中>

与布局,在纵向替换

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fragment_layout_support
机器人:layout_width =match_parent
机器人:layout_height =match_parent><片段
    机器人:ID =@ + ID /标题
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=*** TitlesFragment/>< /&的FrameLayout GT;

和最后我LogCat中

LogCat中截图这里

我以前的教程去之前工作片段。我不能发布更多的链接,但你会发现它在YouTube上被称为ListFragment教程第2部分。

在这一点上我接受任何事情,包括从头开始和不同的做code,但我很想更了解什么在这里,为什么发生,这样我就可以从中吸取教训。谢谢!

修改
尝试新的东西,有一个新的错误

  @覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    showDetails(位置);
}无效showDetails(INT位置){
    SherlockFragment newContent =新SherlockFragment();
    FragmentTransaction交易= getFragmentManager()调用BeginTransaction()。
    transaction.add(R.id.fragment_layout_support,newContent);
    器transaction.commit();
    如果(mDualPane){
        开关(位置){
        情况下0:
            newContent =新蓝();
            打破;
        情况1:
            newContent =新红();
            打破;
        案例2:
            newContent =新绿();
            打破;
        案例3:
            newContent =新黄();
            打破;
        情况4:
            newContent =新紫();
            打破;
    } transaction.add(R.id.fragment_layout_support_land,newContent);
    }其他{
        开关(位置){
        情况下0:
            newContent =新蓝();
            打破;
        情况1:
            newContent =新红();
            打破;
        案例2:
            newContent =新绿();
            打破;
        案例3:
            newContent =新黄();
            打破;
        情况4:
            newContent =新紫();
            打破;
    } transaction.replace(R.id.fragment_layout_support,newContent);
    }
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.addToBackStack(NULL);
    器transaction.commit();
    返回;
}
}

新LogCat中

 致命异常:主要java.lang.IllegalStateException:提交已经叫
在android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
在android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
在[包装] .TitlesFragment.showDetails(TitlesFragment.java:97)
在[包装] .TitlesFragment.onListItemClick(TitlesFragment.java:44)
在android.support.v4.app.ListFragment $ 2.onItemClick(ListFragment.java:58)
在android.widget.AdapterView.performItemClick(AdapterView.java:298)
在android.widget.AbsListView.performItemClick(AbsListView.java:1100)
在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2788)
在android.widget.AbsListView $ 1.run(AbsListView.java:3463)
在android.os.Handler.handleCallback(Handler.java:730)
在android.os.Handler.dispatchMessage(Handler.java:92)
在android.os.Looper.loop(Looper.java:137)
在android.app.ActivityThread.main(ActivityThread.java:5103)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:525)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
在dalvik.system.NativeStart.main(本机方法)


解决方案

您应该你的肖像XML更改为:

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / fragment_layout_support
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    <的FrameLayout
        机器人:ID =@ + ID /标题
        机器人:layout_width =0像素
        机器人:layout_height =match_parent
        机器人:layout_weight =1/>< /&的FrameLayout GT;

而在你的 MainActivity 的onCreate 方法,把:

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    TitlesFragment标题=新TitlesFragment();
    。getSupportFragmentManager()调用BeginTransaction()取代(R.id.titles,标​​题).commit();
}

同时请改回在code TitlesFragment 查看我的 showDetails 之下。

注意:对于风景,我希望你能搞清楚自己。您当前的处理土地画像的方式,海事组织是不是做到这一点的最好办法。阅读布局别名 了解更多详情。

额外:您不必创建蓝色绿色红色类。您可以创建一个彩虹类。

 公共类彩虹扩展SherlockFragment {    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
    }    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.rainbow,集装箱,FALSE);        //默认颜色
        INT颜色= Color.WHITE;        //获取颜色,如果通过setArguments提供的(捆绑参数)
        如果(getArguments()!= NULL){
            。颜色= getArguments()调用getInt(色);
        }        view.findViewById(R.id.rainbow).setBackgroundColor(彩色);        返回视图。
    }
}

更改 showDetails 来:

 无效showDetails(INT位置){
    FragmentManager FM = getFragmentManager();
    FragmentTransaction英尺= fm.beginTransaction();    INT色;
    开关(位置){
    情况下0:
        颜色= Color.BLUE;
        打破;
    情况1:
        颜色= Color.RED;
        打破;
    默认:
        颜色= Color.WHITE;
    }    彩虹彩虹=新彩虹();
    捆绑参数=新包();
    arguments.putInt(色,颜色);
    rainbow.setArguments(参数);    ft.replace(R.id.titles,彩虹);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(NULL);
    ft.commit();
    返回;
}

I've been stuck on this all day, and I can't seem to find an answer here that fits my situation close enough for me to put to use.

Here's a look at my UI, the tabs and listviews function nicely: http://i.stack.imgur.com/84kRY.png

When I click a list item, like Blue, in portrait I want it to display a new fragment being a blue screen, and in landscape it displays that fragment on half the screen, and the listview in the other. I'm using ActionBarSherlock and fragments to accomplish this.

I built it from the inside out, so I started with a single list and it worked as I wanted it to. Then I added the tabs, the pager functionality, and the other list. Somewhere in that process the fragment displaying stopped working, and everytime I click anything I get a crash.

Here's the clicky part of one of my ListView activities

public class TitlesFragment extends SherlockListFragment{

boolean mDualPane;
int mCurCheckPosition = 0;

    .
    .
    .

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    showDetails(position);
}

void showDetails(int position){
    SherlockFragment newContent = new SherlockFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();


    if(mDualPane){
        switch(position){
        case 0:
            newContent = new Blue();
            break;
        case 1:
            newContent = new Red();
            break;
        case 2:
            newContent = new Green();
            break;
        case 3:
            newContent = new Yellow();
            break;
        case 4:
            newContent = new Purple();
            break;


    } ft.replace(R.id.details, newContent);
    } else {
        switch(position){
        case 0:
            newContent = new Blue();
            break;
        case 1:
            newContent = new Red();
            break;
        case 2:
            newContent = new Green();
            break;
        case 3:
            newContent = new Yellow();
            break;
        case 4:
            newContent = new Purple();
            break;


    }ft.replace(R.id.titles, newContent);
    }
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(null);
    ft.commit();
    return;
}
}

Here's the Blue class

public class Blue extends SherlockFragment{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.blue, container, false);
}

}

The Blue layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blue"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000FF"
android:orientation="vertical" >

</LinearLayout>

The layout I want it to replace in landscape

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:id="@+id/fragment_layout_support_land" >

<fragment
    android:id="@+id/titles_land"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="1"
    class="***.TitlesFragment" />

<fragment
    android:id="@+id/details"
    android:layout_width="0px"
    android:layout_height="match_parent"
    android:layout_weight="2" />

</LinearLayout>

And the layout to replace in portrait

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_layout_support"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/titles"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="***.TitlesFragment" />

</FrameLayout>

And last my LogCat

LogCat Screenshot Here

I used a tutorial to get to fragments working before. I can't post more links but you'll find it on youtube called ListFragment Tutorial Part 2.

At this point I'm open to anything, including starting from scratch and doing the code differently, but I'd love even more to understand what is happening here and why, so I can learn from it. Thanks!

EDIT Tried something new and got a new error

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    showDetails(position);
}

void showDetails(int position){
    SherlockFragment newContent = new SherlockFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_layout_support, newContent);
    transaction.commit();


    if(mDualPane){
        switch(position){
        case 0:
            newContent = new Blue();
            break;
        case 1:
            newContent = new Red();
            break;
        case 2:
            newContent = new Green();
            break;
        case 3:
            newContent = new Yellow();
            break;
        case 4:
            newContent = new Purple();
            break;


    } transaction.add(R.id.fragment_layout_support_land, newContent);
    } else {
        switch(position){
        case 0:
            newContent = new Blue();
            break;
        case 1:
            newContent = new Red();
            break;
        case 2:
            newContent = new Green();
            break;
        case 3:
            newContent = new Yellow();
            break;
        case 4:
            newContent = new Purple();
            break;


    }transaction.replace(R.id.fragment_layout_support, newContent);
    }
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.addToBackStack(null);
    transaction.commit();
    return;
}
}

New LogCat

    FATAL EXCEPTION: main java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at [package].TitlesFragment.showDetails(TitlesFragment.java:97)
at [package].TitlesFragment.onListItemClick(TitlesFragment.java:44)
at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
at android.widget.AbsListView$1.run(AbsListView.java:3463)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

解决方案

You should change your portrait xml to:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_layout_support"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</FrameLayout>

And in your MainActivity's onCreate method, put:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    TitlesFragment title = new TitlesFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.titles, title).commit();
}

Also please change back the code in TitlesFragment OR see my showDetails below.

Note : For Landscape, I hope you can figure it out yourself. Your current way of handling land-portrait, IMO is not the best way to do it. Read Layout Aliases for more details.

Extra : You don't have to create Blue, Green, Red, Yellow classes. You can create a Rainbow class.

public class Rainbow extends SherlockFragment {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.rainbow, container, false);

        // default color
        int color = Color.WHITE;

        // get the color if provided through setArguments(Bundle args)
        if (getArguments() != null) {
            color = getArguments().getInt("color");
        }

        view.findViewById(R.id.rainbow).setBackgroundColor(color);

        return view;
    }
}

Change your showDetails to:

 void showDetails(int position) {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    int color;
    switch (position) {
    case 0:
        color = Color.BLUE;
        break;
    case 1:
        color = Color.RED;
        break;
    default:
        color = Color.WHITE;
    }

    Rainbow rainbow = new Rainbow();
    Bundle arguments = new Bundle();
    arguments.putInt("color", color);
    rainbow.setArguments(arguments);

    ft.replace(R.id.titles, rainbow);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.addToBackStack(null);
    ft.commit();
    return;
}

这篇关于未找到ID为片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆