gpt4 book ai didi

android - 类扩展 FrameLayout 并膨胀 FrameLayout

转载 作者:行者123 更新时间:2023-11-29 21:31:46 24 4
gpt4 key购买 nike

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

希望对你有所帮助。

最佳答案

你应该这样做,

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

代替,

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

然后,

如果你想添加一些其他 View ,你可以像下面那样做,

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com