2013-10-09 72 views
0

我想用一個RelativeLayout膨脹一個FrameLayout。然後我伸出的FrameLayout裏是這樣的:類擴展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} 

希望它可以幫助你來幫我。

+1

請張貼您的logcat。 –

+0

在此處張貼...感謝您的關注! –

+0

您可以也應該編輯您的問題,並且不要將其他信息作爲答案發布。 – stema

回答

1

你應該這樣做,

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); 
+0

仍然強制關閉= / –