【发布时间】:2014-07-30 17:06:45
【问题描述】:
我的肖像和风景模式有两种布局。
在 potrait 中我有一个 ListView,在 Landscape 中我有一个 GridView。
布局/myxml.xml
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/getdata"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
layout-land/myxml.xml
<com.etsy.android.grid.StaggeredGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:item_margin="8dp"
app:column_count="3" />
我在我的类中扩展了这些布局,它扩展了一个片段。我想根据方向膨胀两个视图。
这就是我所做的:-
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
Configuration config;
if(config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
View vi = inflater.inflate(R.layout.myxml, container, false);
newsList = (ListView)vi.findViewById(android.R.id.list); //Error Here
itemsAdapter = new LazyAdapter(myContext, getList );
newsList.setAdapter(itemsAdapter);
}
else if(config.orientation ==Configuration.ORIENTATION_LANDSCAPE)
{
View vi = inflater.inflate(R.layout.myxml, container, false);
mGridView =(StaggeredGridView)vi.findViewById(R.id.grid_view);
stagAdaper = new StaggeredAdapter(myContext, android.R.layout.simple_list_item_1, getList);
mGridView.setAdapter(stagAdaper);
}
}
但我得到一个错误:-
Caused by: java.lang.NullPointerException
06-09 10:44:34.369: E/AndroidRuntime(2680): at mypackage.class(HomeFragment.java:220)
【问题讨论】:
标签: android listview gridview orientation