Reputation: 11
I'm trying to extend the EditText class but I can't seem to get it to work. I've created this class in my public class MainActivity extends Activity as a sub class:
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
}
}
and tried hand coding and using the Graphical Layout tab in Eclipse to edit the activity_main.xml to add the new class as such:
<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>
But I get:
java.lang.NullPointerException at com.android.layoutlib.bridge.impl.RenderSessionImpl.getDefaultProperties(RenderSessionImpl.java:1476) 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(PropertyTable.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) at org.eclipse.swt.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.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(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) 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)
from the activity_main.xml and ClassNotFoundException when I run the application. I've googled around and looked at Extending a EditText in Android. What do I do wrong? and What's the right way to extend EditText to give it additional “default” functionality among others to no avail.
Can anyone see where I've gone wrong as I've spent hours looking myself and can't see it.
Upvotes: 0
Views: 409
Reputation: 7708
The reason is because you EditText2
is an inner class. You should reference it as follows () using $
):
<view class="first.words.firstwords.MainActivity$EditText2"
...
...
Alternatively, move this to a new class file (eg: EditText2.java
).
Here are some more suggestions for you to try.
Upvotes: 0