Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 扩展Layoutclass的复合组件的膨胀问题_Android_Components_Composite - Fatal编程技术网

Android 扩展Layoutclass的复合组件的膨胀问题

Android 扩展Layoutclass的复合组件的膨胀问题,android,components,composite,Android,Components,Composite,我正在尝试扩展LinearLayout以用作复合组件。我遵循了本教程: 这似乎相当直截了当。然而,当我试图给我的部件充气时,它总是失败。我从logCat得到的错误是找不到我的类。我觉得奇怪的是,据我所知,android在它自己的组件中搜索我的组件(我不确定这里的正确措辞,请在下面搜索) 我写的代码如下: <merge android:class="com.packageName.UIClassName" android:id="@+id/DialView" xmlns:a

我正在尝试扩展LinearLayout以用作复合组件。我遵循了本教程:

这似乎相当直截了当。然而,当我试图给我的部件充气时,它总是失败。我从logCat得到的错误是找不到我的类。我觉得奇怪的是,据我所知,android在它自己的组件中搜索我的组件(我不确定这里的正确措辞,请在下面搜索)

我写的代码如下:

<merge android:class="com.packageName.UIClassName"
    android:id="@+id/DialView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<EditText android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="test"/>
</merge>
主要(当前活动):

我的扩展线性布局:

public class DialPadView extends LinearLayout {

public DialPadView(Context ctx, AttributeSet attr) {
    super(ctx, attr);
}


@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ((Activity)getContext()).getLayoutInflater().inflate(R.layout.dialpadview, this);
}
}

My main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<whalenut.testinflate.DialPadView
    android:id="@+id/mydialpad"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    /></LinearLayout>

最后是要与复合组件一起使用的dialpadview.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<DialPadView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="test"/>
</DialPadView>

当然,我的所有类文件都位于包whalent.testinflate

logCat说:

java.lang.RuntimeException:无法启动activity ComponentInfo{whalenut.testinflate/whalenut.testinflate.TestInflateActivity}:android.view.InflateException:二进制XML文件行#2:膨胀类DialPadView时出错

原因:android.view.InflateException:二进制XML文件行#2:膨胀类DialPadView时出错

原因:java.lang.ClassNotFoundException:android.view.DialPadView加载程序dalvik.system.PathClassLoader[/data/app/whalenut.testinflate-2.apk]

为什么Dalvik(?)在包android.view中搜索我的类,而不是whalunt.testinflate当我在xml文件中给出了完全限定的类名时,是因为它没有找到它开始吗


简而言之,为什么我不能,或者更确切地说,我如何在android中扩展布局?

您需要按如下方式更改拨号板视图:

<merge android:class="com.packageName.UIClassName"
    android:id="@+id/DialView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<EditText android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="test"/>
</merge>
然后,您可以在主要活动中使用以下内容:

//just the ordinary autogenerated eclipse code...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
DialView myDialView = new DialView(context);

mainView.addView(myDialView);

我希望这有帮助

谢谢你解决了我的问题。但是,您不需要android:class=“…”属性(eclipse不会接受它)。很高兴听到这个消息!很高兴我能帮忙:)