如何实现复合视图(android.view.InflateException错误)

时间:2012-08-09 00:50:22

标签: android

我是Android新手,我试图了解复合视图概念,如果是android。我不确定我的以下实现是否实用,但它是出于学习目的,我从Wrox Android书中获取了这个想法。所以,我有三个组件,即按钮,编辑文本和列表视图。编辑文本的目的是编写一些文本,而列表使用文本更新,按钮是清除按钮,并且假设清除编辑文本。我打算做的是保持清除按钮并在一个视图组中编辑文本。下面是它的java和xml代码。

public class CompoundView extends LinearLayout {

EditText editText;
Button clearButton;

public CompoundView (Context context, AttributeSet attr) {
    super(context, attr);
    String infService = Context.LAYOUT_INFLATER_SERVICE;
    LayoutInflater li;

    li = (LayoutInflater)getContext().getSystemService(infService);
    li.inflate(R.layout.clearable_edit_text, this, true);

    editText = (EditText) findViewById(R.id.editText);
    clearButton = (Button) findViewById(R.id.clearButton);

    hookUpButton ();
}

public void hookUpButton () {

    clearButton.setOnClickListener(new Button.OnClickListener () {
        public void onClick (View v) {
            editText.setText ("");

        }
    });
}

public EditText getEditText() {
    return this.editText;
}

}

XML

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText 
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/clearButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Clear"
    />
</merge>

我强烈认为这种实施很好。在下一部分中,我有MainActivity.java和activity_main.xml。在这方面,我面临着这个问题,而且最有可能做错了。

public class MainActivity extends Activity {

@SuppressLint({ "ParserError", "ParserError" })
public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView myListView = (ListView)findViewById(R.id.myListView);

    final CompoundView textAndClear = (CompoundView) findViewById(R.id.myEditAndClear) ;
    final EditText editText = textAndClear.getEditText();
    final ArrayList<String> toDoItems = new ArrayList<String>();

    final ArrayAdapter<String> aa;
    int resId = R.layout.todolist_item;
    aa= new ArrayAdapter<String> (this, resId, toDoItems);
    myListView.setAdapter(aa);

    editText.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.ACTION_DOWN) || (keyCode == KeyEvent.KEYCODE_ENTER)){
                    toDoItems.add(0,editText.getText().toString());
                    aa.notifyDataSetChanged();
                    editText.setText("");
                    return true;
                }
            return false;
        }
    });
    }
}

和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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.example.wroxexample.CompoundView
    android:id="@+id/myClearableEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_below="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

,错误是

06-29 06:32:11.783: E/AndroidRuntime(5413): FATAL EXCEPTION: main
06-29 06:32:11.783: E/AndroidRuntime(5413): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.wroxexample/com.example.wroxexample.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.os.Looper.loop(Looper.java:137)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.reflect.Method.invokeNative(Native Method)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.reflect.Method.invoke(Method.java:511)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at dalvik.system.NativeStart.main(Native Method)
06-29 06:32:11.783: E/AndroidRuntime(5413): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Activity.setContentView(Activity.java:1835)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at com.example.wroxexample.MainActivity.onCreate(MainActivity.java:20)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Activity.performCreate(Activity.java:4465)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-29 06:32:11.783: E/AndroidRuntime(5413):     ... 11 more
06-29 06:32:11.783: E/AndroidRuntime(5413): Caused by: java.lang.ClassNotFoundException: com.example.wroxexmple.CompoundView
06-29 06:32:11.783: E/AndroidRuntime(5413):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
06-29 06:32:11.783: E/AndroidRuntime(5413):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-29 06:32:11.783: E/AndroidRuntime(5413):     ... 21 more

1 个答案:

答案 0 :(得分:1)

您发布的错误下方应该是异常的真正原因。在我们简单地看一下你的代码之后,我的猜测是,异常来自于你只实现了一个Context的构造函数(当你实例化View时,通常会使用这个构造函数。码)。当您在xml布局中使用复合视图时,将使用也带有AttributeSet的构造函数(而不是您实现的只需要Context的构造函数)。

另外,作为旁注,您可以在View方法中搜索化合物onCreate,然后将其转换为LinearLayout。在onKey回调中,您尝试在其上调用getText()setText(),这些方法未针对类LinearLayout实现(您可能在自定义视图中使用了这些方法)但如果您将视图转换为LinearLayout,则方法将无法使用。)

另外,您可能需要阅读merge标记,以改善化合物View的布局。