Android的 - ListFragment上失败的setContentView [英] Android - ListFragment fails on setContentView

查看:324
本文介绍了Android的 - ListFragment上失败的setContentView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让一个列表视图片段内工作了一整天。我不知道为什么,但出于某种原因,我试着去做到这一点,它只是从来不愿意夸大的看法。即时通讯使用actionbarsherlock如果有差别,但我不这么认为。继承人在那里我的布局失败等级:

 公共类GraffSearch扩展FragmentActivity实现ActionBar.TabListener {@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.searchlayout); //&下; -------------这里失败    最后的动作条AB = getSupportActionBar();    的for(int i = 1; I< 4;我++){
        ab.addTab(ab.newTab()的setText(标签+ I).setTabListener(本));
    }
    ab.show();
    如果(ab.getNavigationMode()!= ActionBar.NAVIGATION_MODE_TABS){
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }}@覆盖
公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){
    // TODO自动生成方法存根}@覆盖
公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){
    //Toast.makeText(this标签的工作+ tab.getPosition()+用户ID为+ avrid,Toast.LENGTH_LONG).show();
    SearchListFrag片段=(SearchListFrag)getSupportFragmentManager()findFragmentById(R.id.searchfrag)。
    //fragment.updateUrl(tab.getPosition(),avrid);}@覆盖
公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){
    // TODO自动生成方法存根}

}

和继承人的布局是我叫我的片段。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
>
< ListFragment
    机器人:ID =@ + ID / searchfrag
    机器人:layout_width =0dip
    机器人:layout_height =match_parent
    机器人:名字=graffit.main.SearchListFrag
    机器人:layout_weight =1>< / ListFragment>< / LinearLayout中>

编辑:我searchlistfrag类

 公共类SearchListFrag扩展片段{ListView的markList;公共SearchListFrag(){}; @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
            捆绑savedInstanceState){
    markList =(ListView控件)气筒
               .inflate(R.id.searchlist,集装箱,FALSE); //< ----------这里失败
     markList.setAdapter(新OrderAdapter(
             MixView.mixContext.getBaseContext(),
             R.layout.searchinnerlistlayout,
             MixView.dataView.dataHandler.markerList));
    //返回inflater.inflate(R.layout.searchlistlayout,集装箱,FALSE);
    返回markList;    }
    @覆盖
    公共无效的onPause(){       super.onPause();
    }    @覆盖
    公共无效onResume(){
        super.onResume();    }    公共无效器updateURL(INT标签,串avrid){    }    私有类OrderAdapter扩展ArrayAdapter<标志物GT; {        私人的ArrayList<标志物GT;分数;        公共OrderAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<标志物GT;项目){
                超(背景下,textViewResourceId,项目);
                this.marks =物品;
        }        @覆盖
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                视图V = convertView;
                如果(V == NULL){
                    LayoutInflater VI =(LayoutInflater)getActivity()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
                    V = vi.inflate(R.layout.searchinnerlistlayout,NULL);
                }
              //订单O = items.get(位置);
                如果(marks.get(位置)!= NULL){
                        TextView的标题=(TextView中)v.findViewById(R.id.title);
                        // TextView中描述=(TextView中)v.findViewById(R.id.description);
                        如果(标题!= NULL){
                              title.setText(标题:+ marks.get(位置).getTitle()); }
                        //如果(描述!= NULL){
                        // description.setText(描述:+ + marks.get(位置)。获得);
                       //}
                        ImageView的图标=(ImageView的)v.findViewById(R.id.markimage);
                        icon.setImageBitmap(marks.get(位置).getBitmap());
                }                返回伏;
        }
}
}

编辑:我searchlistlayout布局

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:方向=垂直
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     >
 < ListView控件
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:名字=@ + ID / searchlist>< /&的ListView GT; //< ----这是我使用id失败 < / LinearLayout中>

伊夫尝试了几种不同的设置要使用常规的片段和具有相同的结果一listfragment来回。继承人的logcat的。

编辑:新的日志猫

  15 03-18:34:45.939:E / AndroidRuntime(20704):致命异常:主要
03-18 15:34:45.939:E / AndroidRuntime(20704):了java.lang.RuntimeException:无法启动活动ComponentInfo {graffit.main / graffit.main.GraffSearch}:android.view.InflateException:二进制XML文件中的行# 9:错误充气类片段
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread.access $ 1500(ActivityThread.java:124)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:968)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.os.Looper.loop(Looper.java:130)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread.main(ActivityThread.java:3806)
03-18 15:34:45.939:E / AndroidRuntime(20704):在java.lang.reflect.Method.invokeNative(本机方法)
03-18 15:34:45.939:E / AndroidRuntime(20704):在java.lang.reflect.Method.invoke(Method.java:507)
03-18 15:34:45.939:E / AndroidRuntime(20704):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 15:34:45.939:E / AndroidRuntime(20704):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 15:34:45.939:E / AndroidRuntime(20704):在dalvik.system.NativeStart.main(本机方法)
03-18 15:34:45.939:E / AndroidRuntime(20704):android.view.InflateException:二进制XML文件行#9:错误充气类片段产生的原因
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.support.v4.app.FragmentActivity.setContentView(FragmentActivity.java:421)
03-18 15:34:45.939:E / AndroidRuntime(20704):在graffit.main.GraffSearch.onCreate(GraffSearch.java:16)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-18 15:34:45.939:E / AndroidRuntime(20704):... 11个
03-18 15:34:45.939:E / AndroidRuntime(20704):$ android.content.res.Resources NotFoundException:由造成资源ID#0x7f050068键入#12H处无效
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.content.res.Resources.loadXmlResourceParser(Resources.java:1912)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.content.res.Resources.getLayout(Resources.java:746)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.inflate(LayoutInflater.java:318)
03-18 15:34:45.939:E / AndroidRuntime(20704):在graffit.main.SearchListFrag.onCreateView(SearchListFrag.java:28)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:660)
03-18 15:34:45.939:E / AndroidRuntime(20704):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-18 15:34:45.939:E / AndroidRuntime(20704):... 19个


解决方案

在布局片段导入是错误的。片段加入到用&LT的布局;片断> 标记。除了与查看你没有命名的完全限定类名后的标记。错误发生,因为Android的尝试一个充气查看的类型'ListFragment,即不存在。

尝试:

 <片段
    机器人:ID =@ + ID / searchfrag
    机器人:layout_width =0dip
    机器人:layout_height =match_parent
    机器人:名字=graffit.main.SearchListFrag
    机器人:layout_weight =1>< /片断>

<片断> 必须是小写,<片断> 不会太

您有些东西混合起来。

1)片段的的onCreate 方法有膨胀的片段的整个布局。充气片段布局后,你可以得到的ListView。

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    查看内容查看= inflater.inflate(R.id.searchlist,集装箱,FALSE);
    markList =(ListView控件)contentView.findViewById(R.id.searchlist);
    markList.setAdapter(新OrderAdapter(MixView.mixContext.getBaseContext(),R.layout.searchinnerlistlayout,MixView.dataView.dataHandler.markerList));    返回内容查看;
}

2)您没有定义的ListView 的id,你定义它的名字。改变布局:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
     机器人:方向=垂直
     机器人:layout_width =match_parent
     机器人:layout_height =match_parent
     >
 < ListView控件
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:ID =@ + ID / searchlist>< /&的ListView GT;
 < / LinearLayout中>

Ive been trying to get a listview working within a fragment all day. I dont know why but for some reason the way Im trying to do it, it just never wants to inflate the view. Im using actionbarsherlock if that makes a difference but I dont think so. Heres the class where my layout fails:

 public class GraffSearch extends FragmentActivity implements ActionBar.TabListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.searchlayout);//<-------------fails here

    final ActionBar ab = getSupportActionBar();

    for (int i = 1; i < 4; i++) {
        ab.addTab(ab.newTab().setText("Tab " + i).setTabListener(this));
    }


    ab.show();
    if (ab.getNavigationMode() != ActionBar.NAVIGATION_MODE_TABS) {
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }



}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    //Toast.makeText(this, "tabs work "+tab.getPosition()+" user id is "+avrid, Toast.LENGTH_LONG).show();
    SearchListFrag fragment = (SearchListFrag) getSupportFragmentManager().findFragmentById(R.id.searchfrag);
    //fragment.updateUrl(tab.getPosition(), avrid);

}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub

}

}

and heres the layout were I call my fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
>


<ListFragment
    android:id="@+id/searchfrag"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:name="graffit.main.SearchListFrag"
    android:layout_weight="1"></ListFragment>



</LinearLayout>

EDIT: my searchlistfrag class

public class SearchListFrag extends Fragment{

ListView markList;

public SearchListFrag(){};

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    markList = (ListView) inflater
               .inflate(R.id.searchlist, container, false);//<----------Fails Here
     markList.setAdapter(new OrderAdapter(
             MixView.mixContext.getBaseContext(),
             R.layout.searchinnerlistlayout, 
             MixView.dataView.dataHandler.markerList ));


    // return inflater.inflate(R.layout.searchlistlayout, container, false);
    return markList;            

    }


    @Override
    public void onPause() {

       super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    public void updateUrl(int tab, String avrid) {

    }

    private class OrderAdapter extends ArrayAdapter<Marker> {

        private ArrayList<Marker> marks;

        public OrderAdapter(Context context, int textViewResourceId, ArrayList<Marker> items) {
                super(context, textViewResourceId, items);
                this.marks = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.searchinnerlistlayout, null);
                }
              //  Order o = items.get(position);
                if (marks.get(position) != null) {
                        TextView title = (TextView) v.findViewById(R.id.title);
                        //TextView description = (TextView) v.findViewById(R.id.description);
                        if (title != null) {
                              title.setText("Title: "+marks.get(position).getTitle());                            }
                        //if(description != null){
                        //  description.setText("Description: "+ +marks.get(position).get);
                       // }
                        ImageView icon = (ImageView) v.findViewById(R.id.markimage);
                        icon.setImageBitmap(marks.get(position).getBitmap());
                }

                return v;
        }
}   
}

EDIT: my searchlistlayout layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >
 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:name="@+id/searchlist"></ListView> //<----this is the id i use that fails

 </LinearLayout>

Ive tried a few different setups going back and forth from using a regular fragment and a listfragment with the same result. Heres the logcat.

EDIT: New log cat

03-18 15:34:45.939: E/AndroidRuntime(20704): FATAL EXCEPTION: main
03-18 15:34:45.939: E/AndroidRuntime(20704): java.lang.RuntimeException: Unable to start activity ComponentInfo{graffit.main/graffit.main.GraffSearch}: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.access$1500(ActivityThread.java:124)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.os.Looper.loop(Looper.java:130)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.main(ActivityThread.java:3806)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at java.lang.reflect.Method.invoke(Method.java:507)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at dalvik.system.NativeStart.main(Native Method)
03-18 15:34:45.939: E/AndroidRuntime(20704): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentActivity.setContentView(FragmentActivity.java:421)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at graffit.main.GraffSearch.onCreate(GraffSearch.java:16)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-18 15:34:45.939: E/AndroidRuntime(20704):    ... 11 more
03-18 15:34:45.939: E/AndroidRuntime(20704): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f050068 type #0x12 is not valid
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:1912)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.content.res.Resources.getLayout(Resources.java:746)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at graffit.main.SearchListFrag.onCreateView(SearchListFrag.java:28)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:845)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1058)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1156)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:660)
03-18 15:34:45.939: E/AndroidRuntime(20704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
03-18 15:34:45.939: E/AndroidRuntime(20704):    ... 19 more

解决方案

The fragment import in your layout is wrong. Fragments are added to a layout with the <fragment> tag. Other than with Views you don't name the tag after the fully-qualified class name. The error occurs because Android tries to inflate a View with the type 'ListFragment', that doesn't exist.

Try:

<fragment
    android:id="@+id/searchfrag"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:name="graffit.main.SearchListFrag"
    android:layout_weight="1"></fragment>

The <fragment> has to be in lower case, <Fragment> won't work too.

Your mixing some things up.

1) The onCreate method of the fragment has to inflate the whole layout of the fragment. After inflating the Fragment layout you can get the ListView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View contentView = inflater.inflate(R.id.searchlist, container, false);     
    markList = (ListView) contentView.findViewById(R.id.searchlist);
    markList.setAdapter(new OrderAdapter(MixView.mixContext.getBaseContext(), R.layout.searchinnerlistlayout, MixView.dataView.dataHandler.markerList));

    return contentView;
}

2) You did not define the id of the ListView, you defined its name. Change the layout:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >
 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/searchlist"></ListView> 
 </LinearLayout>

这篇关于Android的 - ListFragment上失败的setContentView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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