2013-11-21 56 views
0

我試圖擴展EditText類,但我似乎無法讓它工作。我創建了這個類在我的公共類MainActivity擴展活動作爲一個子類:擴展EditText類不會實例化

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中的圖形佈局選項卡編輯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:14 76) at com.android.layoutlib.bridge.BridgeRenderSession.getDefaultProperties(BridgeRenderSession.java:68) at com.android.ide.eclipse.adt.internal.editors.layout.gle2.ViewHierarchy.getDefaultProperties(ViewHierarchy.java: 710) at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getStringValue(XmlProperty.java:209) at com.android.ide.eclipse.adt.internal.editors.layout。 properties.XmlProperty.getValue(XmlProperty.java:221) at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlPropertyEditor.getText(XmlPropertyEditor.java:116) at 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(Pr opertyTable.java:1309) at 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) at 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) at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) at org.eclipse.swt.widgets。 Widget.sendEvent(Widget.java:1276) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1300) at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1285) org.eclip上的 seswt.widgets.Control.gtk_expose_event(Control.java:2992) at org.eclipse.swt.widgets.Composite.gtk_expose_event(Composite.java:709) at org.eclipse.swt.widgets.Canvas.gtk_expose_event( Canvas.java:167) at org.eclipse.swt.widgets.Widget.windowProc(Widget.java:1769) at org.eclipse.swt.widgets.Control.windowProc(Control.java:5116) at org。 eclipse.swt.widgets.Display.windowProc(Display.java:4377) at org.eclipse.swt.internal.gtk.OS._gtk_main_do_event(Native Method) at org.eclipse.swt.internal.gtk.OS.gtk_main_do_event (OS.java:8317) at org.eclipse.swt.widgets.Display.eventProc(Display.java:1193) at org.eclipse.swt.internal.gtk.OS._gdk_window_process_all_updates(Native Method) at org.eclipse.swt.internal.gtk.OS.gdk_window_process_all_updates(OS.java:5571) at org.eclipse.swt.widgets.Display.update(Display.java:4330) at org.eclipse.swt。 widgets.Display.runDeferredLayouts(Display.java:3588) at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3173) 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.eui.internal.app.EclipseAppHandle.run(EclipseAppHandle)org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124) .java:196) at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110) at org.eclipse.core.runtime.in org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353) (位於org.eclipse.core.runtime.adaptor.EclipseStarter)上的ternal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79) 。運行(EclipseStarter.java:180) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本機方法) 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :43) 在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) at org.eclipse.equinox.launcher.Main.run(Main.java:1438) 在org.eclipse.equinox.launcher.Main.main(Main.java:1414)

從activity_main.xml中和的ClassNotFoundException當我運行該應用程序。 我搜索了一下,看着Extending a EditText in Android. What do I do wrong?What's the right way to extend EditText to give it additional 「default」 functionality等無濟於事。

任何人都可以看到我出錯了,因爲我花了幾個小時看着自己,看不到它。

回答

0

原因是因爲你EditText2是一個內部類。你應該引用它作爲使用$如下()):

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

或者,將這種新一類的文件(如:EditText2.java)。

Here是一些更多的建議供您嘗試。

+0

啊,好的,我明白了。我知道我一定是在僞造一些東西。 有趣的一點:使用ViewText的EditText作爲xml標籤仍然會失敗。謝謝Amulya。 –