java - 膨胀 View 问题。尝试向布局添加 View 。类转换异常

标签 java android view

好吧,我不确定我在这里做错了什么。在查找了许多方向之后,我想出了这个 fragment ,但我不确定为什么它不起作用。将不胜感激解释和帮助。包含 LogCat 日志。

activity_camera.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraActivity" >



    <Button
        android:id="@+id/buttonMenu"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/capture"
        android:layout_alignBottom="@+id/capture"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="22dp"
        android:text="menu" />

    <Button
        android:id="@+id/capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:onClick="onClick"
        android:text="Take pic" />


</RelativeLayout>

这是给我一个错误的 fragment

protected void onCreate(Bundle savedInstanceState) {
        mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        super.onCreate(savedInstanceState);

        RelativeLayout RL = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_camera, null);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
        RL.addView(mTextureView);
        setContentView(RL);

日志:

E/AndroidRuntime(  784): FATAL EXCEPTION: main
E/AndroidRuntime(  784): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1723)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1670)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:382)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:403)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
E/AndroidRuntime(  784):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(  784):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(  784):    at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(  784):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(  784):    at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(  784):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  784):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  784):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(  784):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

onCreate 代码更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);

    RelativeLayout RL = (RelativeLayout) getWindow().getDecorView().findViewById(android.R.id.content);
    mTextureView = new TextureView(this);
    mTextureView.setSurfaceTextureListener(this);
    RL.addView(mTextureView);
}

你需要先调用super.onCreate。然后 setContentView 到您的布局(这是文件的 id = 名称,而不是您的相对布局)。然后你就可以在里面找到你的相对布局。

关于java - 膨胀 View 问题。尝试向布局添加 View 。类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19378506/

相关文章:

Android:View 类是否适合图形元素?

java - 自定义格式字符串 : arguments in double brackets java

java - 将文本文件从 URL 直接发送到扫描仪中

java - 应用程序长时间不活动后自动短信不起作用

android - 如何在Stetho中添加、删除和编辑数据?

ios - 覆盖面板 iPad

java - Canvas 中的背景(图像),同时在顶部绘制路径

java - 即使内容是 XML,POST contentType 也始终显示 text/plain

android - 如何打开系统 Activity 并单击那里的按钮?

android - 如何将 View 添加到 XML 布局 android