3

FrameLayout から拡張するカスタム ビューを作成しています。

各 CTOR で、私は LayoutInflater を使用して XML からいくつかのビューをインフレートし、次のようなものを使用しています (「init()」という関数で):

final LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.custom_view, this, true); // <= here there is an NPE 

問題は、アプリがこのコードで正常に動作するにもかかわらず、グラフィカル デザイナーが常にカスタム ビューのレンダリングに失敗し、エラー ログに次のような内容を書き続けることです。

The following classes could not be instantiated:
- com.example.customviewtest.CustomView (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

android.content.res.Resources$NotFoundException: Could not resolve resource value: 0x7F030018.
    at android.content.res.BridgeResources.throwException(BridgeResources.java:693)
    at android.content.res.BridgeResources.getLayout(BridgeResources.java:271)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
    at com.example.customviewtest.CustomView.init(CustomView .java:38)
    at com.example.customviewtest.CustomView.<init>(CustomView .java:26)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(    at sun.reflect.NativeConstructorAccessorImpl.newInstance(    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(    at java.lang.reflect.Constructor.newInstance(    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:422)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:179)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:372)

たまにしか発生しないようで、どのケースで発生するのかわかりません。実際のコードがグラフィカル エディターと同期していないようです。ビューをまったく異なるタイプのビューにキャストしようとしていると主張することさえあります。

私の質問

なぜ発生するのですか?それはADTのバグですか?

インフレータを使用する複数の方法を試しました。

getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)

また、onFinishInflate メソッドで膨張を試みましたが、何も機能しませんでした。

多くの人がこの問題について投稿しているようですが、解決策を見つけた人は誰もおらず、そもそもインフレを無効にするために単に isInEditMode を使用していました。

4

1 に答える 1

1

R クラス ファイルに移動し、0x7F030018を指しているものを見つけます。

編集

追加する必要があります

if (!isInEditMode())
{
   // Your major LayoutInflater codes goes here
}

カスタム ビューのコンストラクターで。その後、ビルドされます。

于 2013-06-27T08:12:37.470 に答える