我正在尝试使用RelativeLayout为FrameLayout充气。然后,我像这样扩展一个FrameLayout:

public class Keyboard extends FrameLayout {

    public Keyboard(Context context){
        this(context, null);

        FrameLayout mainContent = (FrameLayout) findViewById(R.layout.calc_beam_activity);

        RelativeLayout v = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.keyboard,null);

        mainContent.addView(v);

    }

    public Keyboard(Context context, AttributeSet attrs){
        this(context, attrs,0);
    }

    public Keyboard(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }

}


但是出了点问题,当我叫这堂课的时候我得到了足球俱乐部。

日志猫来了

10-09 19:45:58.553      60-88/system_process I/ActivityManager﹕ Displayed com.iregados.beamcalculator/.MainActivity: +1s238ms
10-09 19:46:05.693      359-359/com.iregados.beamcalculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-09 19:46:05.713      359-359/com.iregados.beamcalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ClassCastException: android.widget.FrameLayout
            at com.iregados.beamcalculator.Keyboard.<init>(Keyboard.java:28)
            at com.iregados.beamcalculator.CalcBeam$baseListener.onFocusChange(CalcBeam.java:69)
            at android.view.View.onFocusChanged(View.java:2742)
            at android.widget.TextView.onFocusChanged(TextView.java:6660)
            at android.view.View.handleFocusGainInternal(View.java:2565)
            at android.view.View.requestFocus(View.java:3720)
            at android.view.View.requestFocus(View.java:3671)
            at android.view.View.requestFocus(View.java:3649)
            at android.view.View.onTouchEvent(View.java:4445)
            at android.widget.TextView.onTouchEvent(TextView.java:6817)
            at android.view.View.dispatchTouchEvent(View.java:3885)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
            at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
            at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
10-09 19:46:05.733        60-66/system_process W/ActivityManager﹕ Force finishing activity com.iregados.beamcalculator/.MainActivity
10-09 19:46:06.289        60-71/system_process W/ActivityManager﹕ Activity pause timeout for HistoryRecord{40543fe8 com.iregados.beamcalculator/.MainActivity}
10-09 19:46:17.266        60-71/system_process W/ActivityManager﹕ Activity destroy timeout for HistoryRecord{40543fe8 com.iregados.beamcalculator/.MainActivity}


希望它能帮助您。

最佳答案

你应该做这个,

LayoutInflater.from(context).inflate(R.layout.calc_beam_activity, this);


代替,

FrameLayout mainContent = (FrameLayout) findViewById(R.layout.calc_beam_activity);


然后,

如果您想添加其他视图,可以按照以下步骤进行操作,

RelativeLayout v = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.keyboard,null);
addView(v);

关于android - 类扩展FrameLayout并为FrameLayout充气,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19262562/

10-12 03:49