【发布时间】:2016-03-26 07:33:40
【问题描述】:
我创建了一个自定义视图。它在真实设备上完美运行,但在设计模式下 eclipse 说:
The following classes could not be instantiated:
- com.test.MyBanner (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse
我的代码很简单:
public class MyBanner extends WebView {
public MyBanner(Context context) {
super(context);
}
public MyBanner(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyBanner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if(this.isInEditMode()){
getLayoutParams().height = 80;
}
else{
getLayoutParams().height = 80;
//Logotype----------------------------------------------
RelativeLayout rl = new RelativeLayout(getContext());
rl.setLayoutParams(getLayoutParams());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView logotype = new ImageView(getContext());
logotype.setBackgroundColor(Color.TRANSPARENT);
logotype.setImageResource(R.drawable.ic_launcher);
rl.addView(logotype, rlp);
ViewGroup v = (ViewGroup)MyBanner.this;
v.addView(rl); //If I comment this it works perfect.
}
}
}
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bannerContainer">
<TextView
android:id="@+id/bannerHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/banner_tab_text" />
<com.test.MyBanner android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bannerHeader"/>
</RelativeLayout>
如果我评论 v.addView(rl); eclipse 正确显示我的观点。尽管我使用this.isInEditMode(),但在我看来,eclipse 会读取所有代码。也许,这是一个日食错误。如何让eclipse忽略不符合this.isInEditMode()条件的代码行?
【问题讨论】:
-
听是可以得到答案的链接stackoverflow.com/questions/15423149/…
-
@DhavalkumarSolanki,我已经阅读了这篇文章。我用
isInEditMode(),但是不行! -
抱歉误导
标签: java android eclipse android-layout android-custom-view