gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:19:44 25 4
gpt4 key购买 nike

好吧,我不确定我在这里做错了什么。在查找了许多方向之后,我想出了这个 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/

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