我有 custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyCustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- different views -->
</com.example.MyCustomLayout>
及其类:
public class MyCustomLayout extends LinearLayout {
public MyCustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);
setUpViews();
}
//different methods
}
和活动,其中包括此布局:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
setUpViews();
}
和 my_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.MyCustomLayout
android:id="@+id/section1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<com.example.MyCustomLayout
android:id="@+id/section2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
因此,当我从以下位置删除评论块LayoutInflater.from(context).inflate(R.layout.custom_layout, this, true);
并以图形模式转到 my_activity.xml 时,我遇到了问题。Eclipse 思考然后崩溃。看起来它试图多次夸大我的自定义视图,但我不明白为什么。当我重新启动 Eclipse 时,我在错误日志中收到此错误:java.lang.StackOverflowError