扩展的EditText类不会实例化

时间:2013-11-21 03:42:39

标签: java android

我试图扩展EditText类但我似乎无法让它工作。我在公共类中创建了这个类MainActivity将Activity扩展为子类:

public class EditText2 extends EditText
{

    public EditText2(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public EditText2(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public EditText2(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }       
}

并尝试手动编码并使用Eclipse中的Graphical Layout选项卡编辑activity_main.xml以添加新类:

<first.words.firstwords.MainActivity.EditText2
    android:id="@+id/user_input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="EditText2" >

    <requestFocus />
</first.words.firstwords.MainActivity.EditText2>

但我明白了:

  

显示java.lang.NullPointerException       在com.android.layoutlib.bridge.impl.RenderSessionImpl.getDefaultProperties(RenderSessionImpl.java:1476)       在com.android.layoutlib.bridge.BridgeRenderSession.getDefaultProperties(BridgeRenderSession.java:68)       在com.android.ide.eclipse.adt.internal.editors.layout.gle2.ViewHierarchy.getDefaultProperties(ViewHierarchy.java:710)       在com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getStringValue(XmlProperty.java:209)       在com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getValue(XmlProperty.java:221)       在com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlPropertyEditor.getText(XmlPropertyEditor.java:116)       在com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlPropertyEditor.paint(XmlPropertyEditor.java:131)       at org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawProperty(PropertyTable.java:1309)       在org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawContent(PropertyTable.java:1151)       at org.eclipse.wb.internal.core.model.property.table.PropertyTable.handlePaint(PropertyTable.java:1094)       在org.eclipse.wb.internal.core.model.property.table.PropertyTable.access $ 200(PropertyTable.java:64)       at org.eclipse.wb.internal.core.model.property.table.PropertyTable $ 3.handleEvent(PropertyTable.java:187)       在org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1276)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300)       在org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285)       at org.eclipse.swt.widgets.Control.gtk_expose_event(Control.java:2992)       在org.eclipse.swt.widgets.Composite.gtk_expose_event(Composite.java:709)       在org.eclipse.swt.widgets.Canvas.gtk_expose_event(Canvas.java:167)       在org.eclipse.swt.widgets.Widget.windowProc(Widget.java:1769)       在org.eclipse.swt.widgets.Control.windowProc(Control.java:5116)       在org.eclipse.swt.widgets.Display.windowProc(Display.java:4377)       at org.eclipse.swt.internal.gtk.OS._gtk_main_do_event(原生方法)       在org.eclipse.swt.internal.gtk.OS.gtk_main_do_event(OS.java:8317)       在org.eclipse.swt.widgets.Display.eventProc(Display.java:1193)       在org.eclipse.swt.internal.gtk.OS._gdk_window_process_all_updates(原生方法)       在org.eclipse.swt.internal.gtk.OS.gdk_window_process_all_updates(OS.java:5571)       在org.eclipse.swt.widgets.Display.update(Display.java:4330)       在org.eclipse.swt.widgets.Display.runDeferredLayouts(Display.java:3588)       在org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3173)       at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine $ 9.run(PartRenderingEngine.java:1053)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)       在org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)       在org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)       在org.eclipse.ui.internal.Workbench $ 5.run(Workbench.java:588)       在org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)       在org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)       在org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)       在org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)       在org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)       在org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)       在org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:606)       在org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)       在org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)       在org.eclipse.equinox.launcher.Main.run(Main.java:1438)       在org.eclipse.equinox.launcher.Main.main(Main.java:1414)

运行应用程序时,来自activity_main.xml和ClassNotFoundException的

。 我已经用Google搜索并查看了Extending a EditText in Android. What do I do wrong?What's the right way to extend EditText to give it additional “default” functionality等,但无济于事。

任何人都可以看到我出错的地方,因为我花了好几个小时看自己,却无法看到它。

1 个答案:

答案 0 :(得分:0)

原因是因为EditText2是一个内部类。您应该使用$)将其引用如下():

<view class="first.words.firstwords.MainActivity$EditText2"
...
...

或者,将其移至新的类文件(例如:EditText2.java)。

Here是您可以尝试的更多建议。