3

XML レイアウト (activity_trajectory.xml) で使用しているカスタム ビュー (CoordinateGridView) があります。

  <com.android.catvehicle.CoordinateGridView
        android:id="@+id/coordinateGrid"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

グラフィカル エディターで次のエラーが表示されます: **.CoordinateGridView をインスタンス化できませんでした。

Android グラフィカル エディターのエラー

これは、カスタム ビューのコンストラクタです。

public CoordinateGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);
        hostActivity = (TrajectoryActivity) this.getContext();
}

これは、CoordinateGridView を含むレイアウトを設定しているアクティビティです。

public class TrajectoryActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        removeActionBar();
        setContentView(R.layout.activity_trajectory);
        initializeViews();
        loadSettings();
    }
}

ここの問題についてのアイデアはありますか?カスタム ビューがレイアウト内の他のすべてのビューをブロックしているため、グラフィカル エディターで何も表示されないのはイライラします...

4

2 に答える 2

6

グラフィカル レイアウトでエラーが発生する理由は、グラフィカル エディターが独自の Context を使用してビューをインスタンス化しようとし、ClassCastException が発生するためです。そのためView.isInEditMode()、ビューが編集モードであるかどうかを判別し、不要な手順をスキップするために使用する必要があります。例えば:

public CoordinateGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);
    if (!isInEditMode()) hostActivity = (TrajectoryActivity) this.getContext();
}

を使用する他の場所でも同じものを使用する必要がありますhostActivity

于 2013-07-18T23:28:16.380 に答える
0

Android Holo ColorPickerを使用してもまったく同じ問題がありました。

この問題は、マニフェスト xml ファイルで宣言された API バージョンが原因でした。

android:minSdkVersion="11" を android:minSdkVersion="8" に変更したところ、問題はなくなりました。

于 2013-10-08T13:23:08.353 に答える