ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 找不到用于片段的id的视图

2019-05-29 09:14:28  阅读:179  来源: 互联网

标签:android android-fragments actionbarsherlock listview


我整天都被困在这里,而且我似乎无法在这里找到适合我的情况的答案,足以让我使用.

以下是我的UI,标签和列表视图功能很好:http://i.stack.imgur.com/84kRY.png

当我单击列表项目(如蓝色)时,我希望它显示一个新的片段为蓝色屏幕,并且在横向上它会在屏幕的一半显示该片段,而在另一个屏幕上显示列表视图.我正在使用ActionBarSherlock和片段来实现这一目标.

我是从内到外构建的,所以我从一个列表开始,它按照我的意愿工作.然后我添加了选项卡,寻呼机功能和其他列表.在该过程的某个地方,片段显示停止工作,每次我点击任何东西我都会崩溃.

这是我的一个ListView活动的clicky部分

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;
}
}

这是蓝班

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);
}

}

蓝色布局

<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>

我希望它在横向上替换的布局

<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>

并以纵向替换布局

<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>

最后我的LogCat

LogCat Screenshot Here

我之前使用过教程来获取片段.我无法发布更多链接,但你会在youtube上找到它,名为ListFragment Tutorial Part 2.

在这一点上,我对任何事情持开放态度,包括从头开始并以不同的方式执行代码,但我更希望了解这里发生的事情以及为什么,所以我可以从中学习.谢谢!

编辑
尝试了一些新的错误并得到了新的错误

@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;
}
}

新的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)

解决方法:

您应该将您的肖像xml更改为:

<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>

在MainActivity的onCreate方法中,放置:

@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();
}

另请更改TitlesFragment中的代码或查看下面的showDetails.

注意:对于Landscape,我希望你能自己弄清楚.你现在处理陆地肖像的方式,IMO并不是最好的方法.阅读Layout Aliases了解更多详情.

额外:您不必创建蓝色,绿色,红色,黄色类.您可以创建Rainbow类.

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;
    }
}

将showDetails更改为:

 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;
}

标签:android,android-fragments,actionbarsherlock,listview
来源: https://codeday.me/bug/20190529/1176757.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有