android.widget.EditText无法强制转换为android.view.ViewGroup

时间:2013-12-14 12:54:19

标签: android

我不知道这个布局有什么问题:

  

<EditText 
    android:id="@+id/search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@android:drawable/ic_search_category_default"
    android:hint="Search...">


<ListView 
    android:id="@+id/lv_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:layout_below="@id/search">
</ListView>

<TextView
    android:id="@+id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/lv_data"
    android:layout_marginLeft="20dp"
    android:text="Not found!">
</TextView></EditText></RelativeLayout>

这是代码:

  

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements TextWatcher, OnItemClickListener
{
  private EditText search;
  private ListView lv;
  private DatabaseHelper dbHelper;
  private ArrayAdapter<Kamus> adapter;
  private List<Kamus> ListKamus;

  protected void oncreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      lv = (ListView) findViewById(R.id.lv_data);
      lv.setEmptyView(findViewById(R.id.empty));
      search = (EditText) findViewById(R.id.search);

      dbHelper = DatabaseHelper.getInstance(this);

      setData();

      search.addTextChangedListener(this);
      lv.setOnItemClickListener(this);
  }

  private void setData()
  {
      ListKamus = dbHelper.getAllKamus();

      adapter = new ArrayAdapter<Kamus>(this, android.R.layout.simple_expandable_list_item_1, ListKamus);
      lv.setAdapter(adapter);
  }

  @Override
  public void afterTextChanged(Editable arg0)
  {
  }

  @Override
  public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
  {
  }

  @Override
  public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3)
  {
      adapter.getFilter().filter(s.toString());
  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
  {
      Bundle b = new Bundle();
      b.putString("istilah", adapter.getItem(position).getIstilah());
      b.putString("arti", adapter.getItem(position).getArti());

      Intent i = new Intent(this, ArtiActivity.class);
      i.putExtras(b);
      startActivity(i);
  }
  }

图形布局显示警告:

Exception raised during rendering: android.widget.EditText cannot be cast to android.view.ViewGroup

java.lang.ClassCastException: android.widget.EditText cannot be cast to android.view.ViewGroup
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:756)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:758)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:727)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:373)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:399)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:336)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:333)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:451)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1545)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1302)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:1059)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegatePageChange(LayoutEditorDelegate.java:686)
    at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.pageChange(CommonXmlEditor.java:360)
    at org.eclipse.ui.part.MultiPageEditorPart$2.widgetSelected(MultiPageEditorPart.java:292)
    at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1077)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1062)
    at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:774)
    at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3028)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1749)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:278)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

3 个答案:

答案 0 :(得分:5)

这是因为您关闭了&lt;其他小部件(UI)之后的/ EditText&gt;

因此,只需在 ListView TextView 之后删除该已关闭的代码并执行以下操作:

<EditText 
 android:id="@+id/search"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:drawableRight="@android:drawable/ic_search_category_default"
 android:hint="Search..."/> // Close your Tag here

答案 1 :(得分:2)

EditText 标记未在适当的位置关闭。你有在TextBox 后关闭它。 EditText不能包含其他ui元素。

所以假设像ListView这样的其他元素是EditText的子元素, 所以它说EditText不能转换为ViewGroup

答案 2 :(得分:1)

编辑文字应该关闭,就像它

<EditText 
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_search_category_default"
android:hint="Search..."/>