我解析图片网址论坛json数组并添加它们staggeredgridview所以布局看起来像pinterest :)
用于切换方向域空间到纵向或纵向到土地空间ı创建两个布局文件其中一个是布局端口(它有2列)另一个是布局 - 土地(它有3列)。
当ı尝试切换到端口时,它的传输正确但端口返回到返回ArrayIndexOutOfBoundsException
错误
09-05 20:34:55.412: E/ViewRootImpl(26391): sendUserActionEvent() mView == null
09-05 20:35:00.182: E/dalvikvm(26391): The following issues were seen:
09-05 20:35:04.302: E/AndroidRuntime(26391): FATAL EXCEPTION: main
09-05 20:35:04.302: E/AndroidRuntime(26391): java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.origamilabs.library.views.StaggeredGridView.populate(StaggeredGridView.java:965)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.origamilabs.library.views.StaggeredGridView.onLayout(StaggeredGridView.java:919)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove.onLayout(CustomViewAbove.java:476)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.View.layout(View.java:14618)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewGroup.layout(ViewGroup.java:4491)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2191)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2005)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1218)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4975)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer.doFrame(Choreographer.java:561)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Handler.handleCallback(Handler.java:725)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Handler.dispatchMessage(Handler.java:92)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.os.Looper.loop(Looper.java:137)
09-05 20:35:04.302: E/AndroidRuntime(26391): at android.app.ActivityThread.main(ActivityThread.java:5279)
09-05 20:35:04.302: E/AndroidRuntime(26391): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 20:35:04.302: E/AndroidRuntime(26391): at java.lang.reflect.Method.invoke(Method.java:511)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-05 20:35:04.302: E/AndroidRuntime(26391): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-05 20:35:04.302: E/AndroidRuntime(26391): at dalvik.system.NativeStart.main(Native Method)
公共类MainActivity扩展了Activity {
private static final String TAG_CATEGORY = "catName";
private static final String TAG_ID = "id";
private static String categoryId;
// token ve tokenValue
static String Token = "token";
static String tokenValue = "********";
private static ArrayList<HashMap<String, String>> contactList;
private static String[] urlArray;
SlidingMenu menu;
jsonParser jsonParser;
JSONArray jArray;
ListView lvSiliding;
PARSEJSONCATEGORYCONTENT categoryContent;
PARSEJSONCATEGORY categoryName;
public static StaggeredGridView gridView;
Display display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidth(5);
menu.setFadeDegree(0.0f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setBehindWidth(400);
menu.setMenu(R.layout.sliding_menu);
jsonParser = new jsonParser();
contactList = new ArrayList<HashMap<String, String>>();
lvSiliding = (ListView) findViewById(R.id.listView_sliding_menu);
gridView = (StaggeredGridView) MainActivity.this
.findViewById(R.id.staggeredGridView1);
categoryName = new PARSEJSONCATEGORY();
categoryName.execute();
categoryContent = new PARSEJSONCATEGORYCONTENT();
categoryContent.execute("6");
lvSiliding.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
categoryId = ((TextView) view.findViewById(R.id.categoryID))
.getText().toString();
menu.toggle();
Toast.makeText(getApplicationContext(), categoryId,
Toast.LENGTH_LONG).show();
categoryContent = new PARSEJSONCATEGORYCONTENT();
categoryContent.execute(categoryId);
}
});
}
// parse sliding menu item
private class PARSEJSONCATEGORY extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
String urlCatName = "******";
jArray = jsonParser.getJSONFromUrltoCategoryName(urlCatName, Token,
tokenValue);
try {
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
// Storing each json item in variable
String catName = c.getString(TAG_CATEGORY);
String catID = c.getString(TAG_ID);
Log.d("catName", catName);
Log.d("catID", catID);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATEGORY, catName);
map.put(TAG_ID, catID);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
ListAdapter adapter1 = new SimpleAdapter(MainActivity.this,
contactList, R.layout.list_item, new String[] {
TAG_CATEGORY, TAG_ID }, new int[] {
R.id.categoryName, R.id.categoryID });
lvSiliding.setAdapter(adapter1);
}
}
// parse girdView item
private class PARSEJSONCATEGORYCONTENT extends
AsyncTask<String, Void, String[]> {
protected String[] doInBackground(String... params) {
String identifier = params[0];
String urlCatContent = "*******";
jArray = jsonParser.getJSONFromUrltoCategoryContent(urlCatContent,
Token, tokenValue, identifier, "0", "10");
List<String> list = new ArrayList<String>();
try {
// looping through All Contacts
for (int i = 0; i < jArray.length(); i++) {
JSONObject k = jArray.getJSONObject(i);
// Storing each json item in variable
String catName = k.getString("ipad_URL");
Log.d("ipad_URL", catName);
list.add(catName);
urlArray = new String[list.size()];
list.toArray(urlArray);
}
String arrayLenght = Integer.toString(urlArray.length);
Log.d("arrayLenght", arrayLenght);
} catch (JSONException e) {
e.printStackTrace();
}
return urlArray;
}
protected void onPostExecute(String[] args) {
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
gridView.setItemMargin(margin + 10); // set the GridView margin
gridView.setPadding(margin + 10, margin + 10, margin + 10,
margin + 10); // have the margin on the
// sides as well
StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
R.id.imageView1, urlArray);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
categoryContent.cancel(true);
}
}
}
layout_land
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:staggered="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity" >
<com.origamilabs.library.views.StaggeredGridView
android:id="@+id/staggeredGridView1"
staggered:numColumns="3"
staggered:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
布局端口
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:staggered="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity" >
<com.origamilabs.library.views.StaggeredGridView
android:id="@+id/staggeredGridView1"
staggered:numColumns="2"
staggered:drawSelectorOnTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
答案 0 :(得分:1)
看起来像StaggeredGridView
onSaveInstanceState()
和onRestoreInstanceState()
中的错误。它假定列数保持不变,而布局和纵向资源的列数不同。
对你有用,它是开源的,你可以解决它:)
作为一种快速解决方法,您可以保持两个方向的列数相同,或尝试在SGV上调用setSaveEnabled(false)
,以便不会调用onSaveInstanceState()
。
答案 1 :(得分:0)
根据以下链接找到解决方案:
NullPointerException after screen orientation change #16
将写下解决问题的步骤:
里面有一个for循环,用这个for循环替换它:
for(int i = 0; i<colCount; i++){
//final int offset = top + ((mRestoreOffsets != null)? Math.min(mRestoreOffsets[i], 0) : 0);
int offset = top;
if((mRestoreOffsets != null) && mRestoreOffsets.length == colCount)
offset += Math.min(mRestoreOffsets[i], 0);
mItemTops[i] = (offset == 0) ? mItemTops[i] : offset;
mItemBottoms[i] = (offset == 0) ? mItemBottoms[i] : offset;
}