1

を拡張するカスタム コントロールがありますRelativeLayout。内部にはImageButtonと がありTextViewます。これはコードの一部です:

    public CustomImageButton(Context context) {
            super(context);
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_image_button, this);
            this.setClickable(false);
            setupViewItems();
        }

        public CustomImageButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_image_button, this);
            this.setClickable(false);
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomImageButton);
            setAttributes(attributes);
            setupViewItems();
            attributes.recycle();
        }
    private void setupViewItems() {
            final ImageButton imageButton = (ImageButton) findViewById(R.id.image_button);
            final TextView textView = (TextView) findViewById(R.id.text_view);


            final String text = this.text;
            final int paddingTopPercentage = this.textTopPadding;
            final int textAlignment = this.textAlignment;

            textView.setText(text);
            textView.setTextSize(this.textSize);
            textView.setTextColor(this.textColor);

            ViewTreeObserver vto = imageButton.getViewTreeObserver();
            vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                public boolean onPreDraw() {
                    int imageHeight = imageButton.getMeasuredHeight();
                    int imageWidth = imageButton.getMeasuredWidth();

                    boolean isTextInCorrectSize = true;
                    int textWidth = 0;
                    do {

                        if (!isTextInCorrectSize)
                            textView.setTextSize(textView.getTextSize() - 1f);

                        Rect bounds = new Rect();
                        Paint textPaint = textView.getPaint();
// BUG HERE - I GET A NULL POINTER EXCEPTION WHEN RUNNING getTextBounds function
                        textPaint.getTextBounds(text, 0, text.length(), bounds);
                        //int textHeight = bounds.height();
                        textWidth = bounds.width();
                        isTextInCorrectSize = (imageWidth - textWidth) > 0; 

                    }while(!isTextInCorrectSize);

                    //DOING SOME STUFF HERE
                    return true;
                }
            });
        }

関連するかどうかはわかりませんが、アプリの一部のこのコントロールはレイアウト xml に直接追加され、プログラムでコンポーネントを追加する必要がある場合があります (したがって、2 つの異なるコンストラクター)。カスタム コンポーネントがレイアウト xml を介して追加されている場合、問題はありませんが、コードを介してカスタム コンポーネントを追加すると、getTextBounds 関数を実行すると NullPointerException が発生します (上記のコードを参照)。関数を呼び出すすべてのパラメーターと textView をデバッグして評価すると、null はありません。これを克服できる理由と方法についてのアイデアはありますか?
ありがとう


編集:これはスタックトレースです:

'java.lang.NullPointerException' with message 'null'. Stack trace:
        com.company.product.ui.views.CustomImageButton$1.onPreDraw(CustomImageButton.java:155)
        android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:571)
        android.view.ViewRoot.performTraversals(ViewRoot.java:1259)
        android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
        android.os.Handler.dispatchMessage(Handler.java:99)
        android.os.Looper.loop(Looper.java:123)
        android.app.ActivityThread.main(ActivityThread.java:3687)
        java.lang.reflect.Method.invokeNative(Native Method)
        java.lang.reflect.Method.invoke(Method.java:507)
        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        dalvik.system.NativeStart.main(Native Method)

    *****************
    ****** GMT Time: 17 Feb 2013 13:09:36 GMT
    ****** Application name: AppName
    ****** User agent: (Android 2.3.3)
    ******************************
    ****** Exception type: java.lang.NullPointerException
    ****** Exception message: null
    ****** Trace trace:
        com.company.product.ui.views.CustomImageButton$1.onPreDraw(CustomImageButton.java:155)
        android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:571)
        android.view.ViewRoot.performTraversals(ViewRoot.java:1259)
        android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
        android.os.Handler.dispatchMessage(Handler.java:99)
        android.os.Looper.loop(Looper.java:123)
        android.app.ActivityThread.main(ActivityThread.java:3687)
        java.lang.reflect.Method.invokeNative(Native Method)
        java.lang.reflect.Method.invoke(Method.java:507)
        com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
        com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
        dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

問題は、クラスのコンストラクター(呼び出す)でTextViewのサイズ(境界)を取得しているという事実にあります。setupViewItems()これは、Androidがまだ描画していないため、サイズを決定していないため、実行できません。代わりに、 View.onSizeChanged()メソッドをオーバーライドして、Viewアイテムのサイズを変更/取得する必要があります 。Androidは、初めてビューのサイズを設定すると、常にこのメソッドを呼び出します。

于 2013-02-17T13:39:30.263 に答える