2

我正在尝试使用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);
    }

}

但是出了点问题,当我打电话给这堂课时,我得到了一个 FC。

这里是 logcat

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}

希望它可以帮助你帮助我。

4

1 回答 1

3

你应该做这个,

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);
于 2013-10-09T03:51:21.333 回答