Reputation: 603
I get a strange error in my Android project. Sometimes the app crashes with this IllegalStateException:
11-23 08:20:51.610: E/AndroidRuntime(16714): FATAL EXCEPTION: main
11-23 08:20:51.610: E/AndroidRuntime(16714): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1286)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:294)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.View.measure(View.java:12775)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.LinearLayout.measureVertical(LinearLayout.java:670)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.View.measure(View.java:12775)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.View.measure(View.java:12775)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.LinearLayout.measureVertical(LinearLayout.java:822)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.LinearLayout.onMeasure(LinearLayout.java:563)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.View.measure(View.java:12775)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4709)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-23 08:20:51.610: E/AndroidRuntime(16714): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2240)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.View.measure(View.java:12775)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1117)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.os.Looper.loop(Looper.java:137)
11-23 08:20:51.610: E/AndroidRuntime(16714): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-23 08:20:51.610: E/AndroidRuntime(16714): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 08:20:51.610: E/AndroidRuntime(16714): at java.lang.reflect.Method.invoke(Method.java:511)
11-23 08:20:51.610: E/AndroidRuntime(16714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-23 08:20:51.610: E/AndroidRuntime(16714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-23 08:20:51.610: E/AndroidRuntime(16714): at dalvik.system.NativeStart.main(Native Method)
I know that this should be caused by two widgets in a RelativeLayout referencing each other, but here it's definitely not the case. I am using a RelativeLayout in this layout file (at the bottom):
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="450dp" >
<fragment
android:id="@+id/identification_navigation_fragment"
android:name="plt.demonstrator.common.NavigationFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:id="@+id/view1"
android:layout_width="3dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:background="@color/lightgray" />
<fragment
android:id="@+id/identification_fragment"
android:name="plt.demonstrator.identifikation.IdentificationFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3.5" />
</LinearLayout>
<RelativeLayout
android:id="@+id/bottom_actionbar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:background="#DDDDDD" >
</RelativeLayout>
Items are added dynamically at runtime, either on left or right side. So actually no item from left side is referencing something on the right side and vice-versa.
Also I cannot identify any part of my code causing this exception, the debugger runs until onResume()
has finished and the app is in resumed state. Here is the code which adds the items, but as I said, the debugger doesn't argue with this...
public static void addActionbarItem(RelativeLayout actionbar, Context ctx, int itemid, int textid,
int iconid, Direction direction, View.OnClickListener listener) {
TextView item = new TextView(ctx);
item.setText(ctx.getString(textid).toUpperCase());
item.setTextAppearance(ctx, android.R.style.TextAppearance_Medium);
int paddingPx = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, ctx.getResources().getDisplayMetrics());
item.setPadding(paddingPx, 0, paddingPx, 0);
item.setGravity(Gravity.CENTER_VERTICAL);
item.setId(itemid);
Drawable icon = ctx.getResources().getDrawable(iconid);
icon.setBounds(0, 0, 24, 24);
item.setCompoundDrawables(icon, null, null, null);
RelativeLayout.LayoutParams separatorParams = new LayoutParams(1, 32);
// vertical separator
View separator = new View(ctx);
int color = ctx.getResources().getColor(android.R.color.secondary_text_light);
separator.setBackgroundColor(color);
separator.setId(separatorId++);
separatorParams.setMargins(0, 8, 0, 0);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
if (direction == Direction.LEFT) {
// Item should be added at left side
if (leftAnchor != -1) {
// There is already an item at left side -> place the new item right to it
separatorParams.addRule(RelativeLayout.RIGHT_OF, leftAnchor);
separator.setLayoutParams(separatorParams);
actionbar.addView(separator);
params.addRule(RelativeLayout.RIGHT_OF, separator.getId());
} else {
// Item is the first on left side, place it at left edge
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, -1);
// Add some space to the display edge
params.setMargins(8, 0, 0, 0);
}
// Update anchor ID
leftAnchor = itemid;
} else {
// Item should be added at right side
if (rightAnchor != -1) {
// There is already an item at right side -> place the new item left to it
separatorParams.addRule(RelativeLayout.LEFT_OF, rightAnchor);
separator.setLayoutParams(separatorParams);
actionbar.addView(separator);
params.addRule(RelativeLayout.LEFT_OF, rightAnchor);
} else {
// Item is the first on right side, place it at right edge
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, -1);
// Add some space to the display edge
params.setMargins(0, 0, 8, 0);
}
// Update anchor ID
rightAnchor = itemid;
}
item.setLayoutParams(params);
// Set item behaviour
item.setOnClickListener(listener);
// Add a touch listener for visual feedback
// Touch listener is invoked before click listener, so event must be forwarded after onTouch method
item.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(final View v, MotionEvent e) {
// ICS clicked button style
v.setBackgroundColor(0x880099CC);
v.invalidate();
Runnable timer = new Runnable() {
public void run() {
// Reset color
v.setBackgroundColor(Color.TRANSPARENT);
v.invalidate();
}
};
Handler h = new Handler();
h.removeCallbacks(timer);
// Reset button style after 50ms
h.postDelayed(timer, 50);
// Forward event to OnClickListener
return false;
}
});
actionbar.addView(item);
}
Has anybody already faced something like this? Any help is greatly appreciated...
Upvotes: 1
Views: 1212
Reputation: 38168
Print out your ids in the logcat, for all the views you add dynamically, you will sure find a view that defines a circular dependency in your layout. Your code is not easy to follow but your error is there.
Upvotes: 2