gpt4 book ai didi

android - java.lang.ArrayIndexOutOfBoundsException while switch portrait and landscape (交错 GridView 库)

转载 作者:行者123 更新时间:2023-11-30 03:23:41 25 4
gpt4 key购买 nike

我解析图像 url 论坛 json 数组并将它们添加到 staggeredgridview 所以布局看起来像 pinterest :)

为了将 Orientation landspace 切换为 portrait 或 portrait to landspace ı 创建两个布局文件,其中一个是 layout-port(它有 2 列)另一个是 layout-land(它有 3 列)。

当我尝试切换陆地到港口时,它是正确的,但港口到陆地返回 ArrayIndexOutOfBoundsException

landspace

portrait

错误

    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)

公共(public)类 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);

}

}

}

布局土地

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

最佳答案

看起来像是 StaggeredGridView 中的错误onSaveInstanceState()onRestoreInstanceState()。它假定列数保持不变,而布局和纵向资源的列数不同。

对你来说很好,它是开源的,你可以修复它:)

作为一种快速解决方法,您可以使两个方向的列数保持相同,或者尝试在 SGV 上调用 setSaveEnabled(false),这样 onSaveInstanceState() 就不会接到电话。

关于android - java.lang.ArrayIndexOutOfBoundsException while switch portrait and landscape (交错 GridView 库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18642636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com