Answer the question
In order to leave comments, you need to log in
Transition between Activity in ActivityGroup (in tabs)?
When switching between activities (using ActivityGroup in tabs), an error occurs:
04-26 08:46:04.599: ERROR/AndroidRuntime(992): java.lang.StackOverflowError<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.text.Layout.measureText(Layout.java:1601)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.text.Layout.getLineMax(Layout.java:655)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.text.Layout.draw(Layout.java:311)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.text.BoringLayout.draw(BoringLayout.java:365)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.widget.TextView.onDraw(TextView.java:4052)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.view.View.draw(View.java:6535)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.view.ViewGroup.drawChild(ViewGroup.java:1531)<br/>
04-26 08:46:04.599: ERROR/AndroidRuntime(992): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)<br/>
...<br/>
...<br/>
private ListView _lv;<br/>
...<br/>
@Override<br/>
public void onCreate(Bundle savedInstanceState) {<br/>
super.onCreate(savedInstanceState);<br/>
...<br/>
List cats = Category.getAllCategories(this);<br/>
_lv = (ListView)findViewById(android.R.id.list);<br/>
_lv.setAdapter(new CategoryAdapter(getBaseContext(),R.layout.list_item_layout, cats)); <br/>
_lv.setOnItemClickListener(new ListClickListener());<br/>
...<br/>
}<br/>
public void replaceContentView(String id, Intent newIntent/*,int result*/) {<br/>
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) <br/>
.getDecorView(); <br/>
this.setContentView(view);<br/>
} <br/>
<br/>
<br/>
Нужный кусок взят отсюда: <a href="http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity">www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity</a><br/>
<br/>
Код лэйаутов:<br/>
<code><?xml version="1.0" encoding="utf-8"?><br/>
<LinearLayout<br/>
xmlns:android="http://schemas.android.com/apk/res/android"<br/>
android:id="@+id/recipes_in_cat_list"<br/>
android:orientation="vertical"<br/>
android:layout_width="fill_parent"<br/>
android:layout_height="fill_parent"><br/>
<LinearLayout<br/>
android:layout_width="fill_parent"<br/>
android:layout_height="wrap_content"<br/>
android:gravity="left" <br/>
android:background="@drawable/tab_upper_bar"><br/>
<Button<br/>
android:id="@+id/back_to_cats_button"<br/>
android:layout_width="wrap_content"<br/>
android:layout_height="wrap_content"<br/>
android:layout_marginLeft="5dip"<br/>
android:background="@drawable/android_info_button" /><br/>
</code><br/>
<ListView<br/>
android:id="@android:id/list"<br/>
android:layout_width="fill_parent"<br/>
android:layout_height="fill_parent"<br/>
android:gravity="center"<br/>
android:divider = "#00000000"<br/>
android:cacheColorHint="#00000000" /><br/>
Answer the question
In order to leave comments, you need to log in
In general, I meant, post the project archive, if it is not secret, somewhere for download, or on some github :) Understand code snippets to find what causes stack overflow, without debugging and reading logs , hard. However, there are a few notes:
Alas, little is clear from the above code. The example from the site works with a bang, so you have a problem. It's possible that using too many LinearLayouts instead of RelativeLayouts, coupled with not caching your Views in the CategoryAdapter's getView method (I don't see how you implemented it), is causing a stack overflow. Either try to optimize your templates, or post a more complete version of the project somewhere for further help.
Promised code:
CategoryAdapter in full:
public class CategoryAdapter extends ArrayAdapter {
private List _items;
public CategoryAdapter(Context context, int textViewResourceId,
List items) {
super(context, textViewResourceId, items);
_items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_layout, null);
v.destroyDrawingCache();
}
Category cat = _items.get(position);
if (cat != null) {
TextView listText = (TextView) v.findViewById(R.id.list_text);
listText.setText(cat.getName());
}
return v;
}
@Override
public Category getItem(int position) {
return _items.get(position);
}
}
Отсюда(таб) мы начинаем переход:
public class RecipeTab extends ActivityGroup {
private ListView _lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.recipe_tab);
List cats = Category.getAllCategories(this);
_lv = (ListView)findViewById(android.R.id.list);
_lv.setAdapter(new CategoryAdapter(getBaseContext(),R.layout.list_item_layout, cats));
_lv.setOnItemClickListener(new ListClickListener());
}
public void replaceContentView(String id, Intent newIntent/*,int result*/) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
}
...
код обработчика при нажатии на элемент списка:
private class ListClickListener implements ListView.OnItemClickListener {
public void onItemClick(AdapterView parent, View v, int position, long l){
Category selectedItem = (Category)_lv.getItemAtPosition(position);
int catId = selectedItem.getId();
Intent intent = new Intent(RecipeTab.this,CategoryActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("catId", catId);
intent.putExtras(bundle);
replaceContentView("categoryActivity", intent);
}
}
...
}
В данный активити мы переходим и потом обратно на RecipeTab
public class CategoryActivity extends Activity{
private ListView _lv;
private Button _btBack;
private LocalActivityManager lam;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.category_activity_layout);
_btBack = (Button)findViewById(R.id.back_to_cats_button);
_btBack.setOnClickListener(new BackButtonListener());
List recps = Recipe.getRecipesByCatId(this, catId);
_lv = (ListView)findViewById(android.R.id.list);
_lv.setAdapter(new RecipeAdapter(getBaseContext(),R.layout.recipe_list_item, recps));
}
и код обработчика:
class BackButtonListener implements View.OnClickListener {
public void onClick(View v){
Intent intent = new Intent(v.getContext(), RecipeTab.class);
RecipeTab parentActivity = (RecipeTab)getParent();
parentActivity.replaceContentView("recipeTab", intent);
}
}
Код лэйаутов для CategoryAcivity и элемента списка находится в вопросе выше.
По-моему, много букв получилось, так что остальное по требованию.
Кстать, кнопка назад в эмуляторе почему-то переходит не на предидущий активити, а на рабочий стол.
Почему так?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question