我一直试图让listview整天在片段中工作。我不知道为什么但由于某种原因我试图这样做,它只是永远不想膨胀视图。我使用actionbarsherlock,如果这有所作为,但我不这么认为。继承我布局失败的班级:
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
}
}
并且继承布局是我称之为片段。
<?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>
编辑:我的searchlistfrag类
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;
}
}
}
编辑:我的searchlistlayout布局
<?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>
我尝试了一些不同的设置来回使用常规片段和具有相同结果的listfragment。这是logcat。
编辑:新的日志猫
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
答案 0 :(得分:4)
布局中的片段导入错误。片段将添加到包含<fragment>
标记的布局中。除了Views
之外,您没有在完全限定的类名后面标记标记。发生此错误是因为Android尝试使用“ListFragment”类型来膨胀View
,而该类型不存在。
尝试:
<fragment
android:id="@+id/searchfrag"
android:layout_width="0dip"
android:layout_height="match_parent"
android:name="graffit.main.SearchListFrag"
android:layout_weight="1"></fragment>
<fragment>
必须为小写,<Fragment>
也不会有效。
你混合了一些东西。
1)片段的onCreate
方法必须使片段的整个布局膨胀。在膨胀Fragment
布局后,您可以获得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)你没有定义ListView
的id,你定义了它的名字。更改布局:
<?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>