Android LayoutInflater初始化RelativeLayout和LinearLayout的问题。

YU_Z_W 发布于 2013/08/13 23:34
阅读 4K+
收藏 0

我自定义一个MyListView extends ListView, 在自定义的MyListView的构造函数里使用LayoutInflater初始化一个布局为RelativeLayout布局,然后获取这个RelativeLayout布局的宽和高时报错,使用LinearLayout就能够正常获取。

不知道这是什么情况。

MainActivity.java:

package com.fyfeng.listviewtest;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

	}

}

主配置文件activity_main.xml:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFD700"
    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=".MainActivity" >

    <com.fyfeng.listviewtest.MyListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </com.fyfeng.listviewtest.MyListView>

</RelativeLayout>
自定义的MyListView extends ListView



package com.fyfeng.listviewtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MyListView extends ListView {
	
	private static final String tag = MyListView.class.getSimpleName();

	private int mHeaderWidth;
	private int mHeaderHeight;
	
	public MyListView(Context context) {
		super(context);
		this.init();
	}

	public MyListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.init();
	}

	public MyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init();
	}

	private void init() {
		LayoutInflater lif = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View header = lif.inflate(R.layout.header, null);

		this.addHeaderView(header);
		
		measureView(header);
		mHeaderWidth = header.getMeasuredWidth();
		mHeaderHeight = header.getMeasuredHeight();
		Log.d(tag, "header width = " + mHeaderWidth + ", height = " + mHeaderHeight);
		
	}

	// 计算headView的width及height值
	private void measureView(View child) {

		ViewGroup.LayoutParams p = child.getLayoutParams();

		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}

		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;

		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		}

		child.measure(childWidthSpec, childHeightSpec);
	}
}


private void measureView(View child) ;这个方法是直接从osc客户端源码里拷贝过来的。


ListView的headerView配置header.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        />

</RelativeLayout>

初始化完这个header布局后,调用measureView(View child)方法就会报空指针异常:

08-13 23:27:37.007: D/AndroidRuntime(1337): Shutting down VM
08-13 23:27:37.007: W/dalvikvm(1337): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
08-13 23:27:37.007: E/AndroidRuntime(1337): Uncaught handler: thread main exiting due to uncaught exception
08-13 23:27:37.017: E/AndroidRuntime(1337): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fyfeng.listviewtest/com.fyfeng.listviewtest.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class java.lang.reflect.Constructor
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.os.Looper.loop(Looper.java:123)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread.main(ActivityThread.java:4203)
08-13 23:27:37.017: E/AndroidRuntime(1337): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 23:27:37.017: E/AndroidRuntime(1337): at java.lang.reflect.Method.invoke(Method.java:521)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
08-13 23:27:37.017: E/AndroidRuntime(1337): at dalvik.system.NativeStart.main(Native Method)
08-13 23:27:37.017: E/AndroidRuntime(1337): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class java.lang.reflect.Constructor
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.Activity.setContentView(Activity.java:1620)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.fyfeng.listviewtest.MainActivity.onCreate(MainActivity.java:20)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
08-13 23:27:37.017: E/AndroidRuntime(1337): ... 11 more
08-13 23:27:37.017: E/AndroidRuntime(1337): Caused by: java.lang.reflect.InvocationTargetException
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.fyfeng.listviewtest.MyListView.<init>(MyListView.java:30)
08-13 23:27:37.017: E/AndroidRuntime(1337): at java.lang.reflect.Constructor.constructNative(Native Method)
08-13 23:27:37.017: E/AndroidRuntime(1337): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
08-13 23:27:37.017: E/AndroidRuntime(1337): ... 21 more
08-13 23:27:37.017: E/AndroidRuntime(1337): Caused by: java.lang.NullPointerException
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:427)
08-13 23:27:37.017: E/AndroidRuntime(1337): at android.view.View.measure(View.java:7703)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.fyfeng.listviewtest.MyListView.measureView(MyListView.java:65)
08-13 23:27:37.017: E/AndroidRuntime(1337): at com.fyfeng.listviewtest.MyListView.init(MyListView.java:39)
08-13 23:27:37.017: E/AndroidRuntime(1337): ... 25 more
08-13 23:27:37.027: I/dalvikvm(1337): threadid=7: reacting to signal 3
08-13 23:27:37.027: E/dalvikvm(1337): Unable to open stack trace file '/data/anr/traces.txt': Permission denied


如果把header.xml中的RelativeLayout改为LinearLayout一切正常,能够获取宽和高。

谁遇到这种问题?有解的赐教一下。

加载中
0
刎枫

你build的api版本是多少?

建议将MyListView 的 init()中,measure相关方法放到onMeasure里面。那样应该就没啥问题了

0
YU_Z_W
YU_Z_W
我是在android1.6上面运行的。
0
YU_Z_W
YU_Z_W

为什么使用LinearLayout这个布局就没问题呢? RelativeLayout和LinearLayout的构建方式不一样?

翻了下源码还没找出问题所在。

有明白的人解释一下.3Q

0
xesam
xesam

ViewGroup.LayoutParams p = child.getLayoutParams();

		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}
这个地方先确保有LayoutParam比较靠谱

AbsListView.LayoutParams p = (AbsListView.LayoutParams) child.getLayoutParams();

if (p == null) {
    p = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
    child.setLayoutParams(p);
}



0
l
ljz2009y

引用来自“YU·ZW”的答案

为什么使用LinearLayout这个布局就没问题呢? RelativeLayout和LinearLayout的构建方式不一样?

翻了下源码还没找出问题所在。

有明白的人解释一下.3Q

你的这个问题的原因在此:

In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead.

YU_Z_W
YU_Z_W
3Q
返回顶部
顶部