一些问题

1.最近遇到的一个问题:java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class ...... instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0xff0101. Make sure other views do not use the same id.

当切换语言时,activity的onRestoreInstanceState方法报错。

代码示例:

布局文件

<com.example.sameidtest.SameIdView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sameid_view_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/switch_lan_to_zh_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClickButton"
            android:text="@string/switch_lan_to_zh" />

        <Button
            android:id="@+id/switch_lan_to_en_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClickButton"
            android:text="@string/switch_lan_to_en" />
    </LinearLayout>

</com.example.sameidtest.SameIdView>
自定义的SameIdView,包含一个代码添加的view和一个布局添加的view

package com.example.sameidtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SameIdView extends RelativeLayout {

	TextView textView;

	public SameIdView(Context context) {
		this(context, null);
	}

	public SameIdView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SameIdView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		textView = new TextView(context, attrs, defStyle);
		textView.setText(R.string.hello_world);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_IN_PARENT);

		addView(textView, lp);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		int viewGroupId = getId();
		int textViewId = textView.getId();
		int viewId = getChildAt(1).getId();
		Log.e("SameId", "viewGroupId = " + viewGroupId + "  textViewId = "
				+ textViewId + "   viewId = " + viewId);
	}

}

log为01-18 09:57:18.322: E/SameId(18563): viewGroupId = 2131099648  textViewId = 2131099648   viewId = -1

可以看出通过new TextView(context, attrs, defStyle);这种方法创建的view id和父控件的一样。

2.切换语言

切换语言代码很简单

public void switchLanguage(Locale locale) {
		try {
			IActivityManager am = ActivityManagerNative.getDefault();
			Configuration config = am.getConfiguration();
			config.locale = locale;
			am.updateConfiguration(config);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
导入到eclipse会报错,因为IActivityManager和ActivityManagerNative是api隐藏的类。将源码下编译的classes.jar包导入即可。
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="lib" path="framework/classes.jar"/>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="src" path="gen"/>
	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
	<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
	<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
	<classpathentry kind="output" path="bin/classes"/>
</classpath>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值