使用 addView() 时我的应用程序崩溃
Posted
技术标签:
【中文标题】使用 addView() 时我的应用程序崩溃【英文标题】:My app crashes when using addView() 【发布时间】:2014-03-15 00:27:20 【问题描述】:我是 android 的菜鸟。每当我使用 ViewGroup addView() 方法时,我的应用程序总是崩溃,但在我使用 Activity addContentView() 方法时可以正常工作。
我想使用 addView() 而不是 addContentView() 的原因是我希望能够在不需要时删除布局,而 addContentView() 是不可能的。
代码: MainActivity.java
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
//CAMERA VIEW
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
ViewGroup rootView = (ViewGroup) findViewById(R.layout.activity_main);
LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
//GRAPHICS VIEW
glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
glSurfaceView.setRenderer(new GLRenderer());
//crash
//rootView.addView(glSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//not crash
addContentView(glSurfaceView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
glSurfaceView.setZOrderMediaOverlay(true);
//CONTROLS VIEW
View ctrlLayout = mLayoutInflater.inflate(R.layout.activity_controls, rootView);
//crash
//rootView.addView(ctrlLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//not crash
addContentView(ctrlLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
ctrlLayout.bringToFront();
textView = (TextView)ctrlLayout.findViewById(R.id.text);
rtTextView = (TextView)ctrlLayout.findViewById(R.id.rt_text);
btnStart = (Button)ctrlLayout.findViewById(R.id.button1);
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_
android:layout_ />
</RelativeLayout>
activity_controls.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/text"
android:layout_
android:layout_
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="@string/hello_world" />
<TextView
android:id="@+id/rt_text"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="18dp"
android:layout_marginTop="14dp"
android:text="@string/str_rttext" />
<TextView
android:id="@+id/title"
android:layout_
android:layout_
android:layout_alignTop="@+id/rt_text"
android:layout_centerHorizontal="true"
android:text="@string/str_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/btn_start" />
</RelativeLayout>
编辑:
我按照 FD_ 的建议更改了我的代码
ViewGroup rootView = mLayoutInflater.inflate(R.layout.activity_main, null); 现在,当我在 onCreate 函数末尾调用 setContentView(rootView) 时,它会崩溃。
这里是日志:
02-16 08:00:28.142: I/dalvikvm(2153): threadid=1: stack overflow on call to Landroid/view/View;.isLayoutDirectionInherited:Z
02-16 08:00:28.142: I/dalvikvm(2153): method requires 12+20+4=36 bytes, fp is 0xb02fe320 (32 left)
02-16 08:00:28.152: I/dalvikvm(2153): expanding stack end (0xb02fe300 to 0xb02fe000)
02-16 08:00:28.152: I/dalvikvm(2153): Shrank stack (to 0xb02fe300, curFrame is 0xb0303ec4)
02-16 08:00:28.152: D/AndroidRuntime(2153): Shutting down VM
02-16 08:00:28.152: W/dalvikvm(2153): threadid=1: thread exiting with uncaught exception (group=0xb1aeab90)
02-16 08:00:28.932: D/dalvikvm(2153): GC_FOR_ALLOC freed 117K, 6% free 3266K/3448K, paused 80ms, total 86ms
02-16 08:00:29.382: D/dalvikvm(2153): GC_FOR_ALLOC freed 356K, 11% free 3423K/3844K, paused 46ms, total 47ms
02-16 08:00:29.402: E/AndroidRuntime(2153): FATAL EXCEPTION: main
02-16 08:00:29.402: E/AndroidRuntime(2153): Process: com.example.previewcamera, PID: 2153
02-16 08:00:29.402: E/AndroidRuntime(2153): java.lang.***Error
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5713)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:5714)
02-16 08:00:29.402: E/AndroidRuntime(2153): at android.view.ViewGroup.resetRes
02-16 08:00:29.632: D/dalvikvm(2153): GC_FOR_ALLOC freed 548K, 16% free 3386K/4000K, paused 54ms, total 55ms
【问题讨论】:
【参考方案1】:对于最终在这里尝试使用自定义视图追踪该错误的其他人,请仔细检查您是否正确充气。以下更改解决了我的问题。
错误:
View view = inflate(getContext(), R.layout.foo, this);
addView(view);
工作正常:
View view = inflate(getContext(), R.layout.foo, null);
addView(view);
【讨论】:
实际使用View view = inflate(getContext(), R.layout.foo, this, false);是正确的方法。【参考方案2】:您必须先致电setContentView()
,然后才能在您的Activity
中使用findViewById()
。此外,findViewById()
返回包含在内容视图中并由作为参数传递的 id 值标识的视图,因此传递布局资源永远不会起作用。
另一种选择是使用 LayoutInflater
自己膨胀视图:
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup rootView = inflater.inflate(R.layout.main, null);
【讨论】:
完成了这项工作。我的应用程序不再崩溃,但现在 addView() 没有显示任何内容。有什么建议吗? 我已经尝试将 glSurfaceView 和 ctrlView 的布局参数更改为 WRAP_CONTENT 但仍然无法正常工作。 如果您使用上述方法(LayoutInflater),您将不得不调用 setContentView(rootView);在 onCreate 结束时。 是的,我正在做你的方法。我试图调用 setContentView(rootView) 但是当我调用它时我的应用程序崩溃了。 您似乎正在添加一个 View 作为其自身的子项,但我无法告诉您在哪里,因为我没有代码。【参考方案3】:我在使用 attachToRoot = true 时遇到了同样的问题
final View submitButton = LayoutInflater.from(this).inflate
(R.layout.view_button_simple, rootView, true);
我不得不把它改成
final View submitButton = LayoutInflater.from(this).inflate
(R.layout.view_button_simple, rootView, false);
【讨论】:
【参考方案4】:Accessign 错误的布局也会导致应用崩溃。检查您是否在弹出布局中添加了具有相同 id 的布局
【讨论】:
以上是关于使用 addView() 时我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章