Android - 匿名内部类型在编辑模式下引发错误

时间:2011-09-02 15:16:12

标签: android

我需要帮助。我尝试扩展按钮类。一切都很好,但如果我添加匿名类型我在设计模式中有错误!以下代码段:

public class ExtDatePicker extends Button { 
private int mYear;
private int mMonth;
private int mDay;
private View view = new View(getContext());
private DatePickerDialog.OnDateSetListener mDateSetListener;

public ExtDatePicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    Init();
}

private void Init(){
    // add a click listener to the button
    this.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog();
        }
    });

    // the callback received when the user "sets" the date in the dialog
    if(!view.isInEditMode()){

/********************** THIS CODE PROVOKE ERROR *****************************/
    mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
             }; 

/****************************************************************************/      
    }       
    GetCurrentDate();
    updateDisplay();
}

// get the current date
private void GetCurrentDate(){
    Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
}

// updates the date in the TextView
private void updateDisplay() {
    String day = String.valueOf(mDay);
    String month = String.valueOf(mMonth+1);
    if(day.length()==1) day="0"+day;
    if(month.length()==1) month="0"+month;
    this.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(day).append(".")
                .append(month).append(".")                    
                .append(mYear));
}    

private void showDialog() {
    DatePickerDialog dialog = new DatePickerDialog(SharedClass.currentContext,
            mDateSetListener, mYear, mMonth, mDay);
    dialog.show();
}       

}

在运行时模式下效果很好!

错误:

java.lang.ClassNotFoundException: android.app.DatePickerDialog$OnDateSetListener
    at com.android.ide.eclipse.adt.internal.resources.manager.ProjectClassLoader.findClass(ProjectClassLoader.java:94)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.getConstructor(Unknown Source)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:262)
    at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:132)
    at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:198)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:126)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:296)
    at com.android.layoutlib.bridge.Bridge.computeLayout(Bridge.java:397)
    at com.android.ide.common.rendering.LayoutLibrary.createLegacySession(LayoutLibrary.java:404)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:285)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1506)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1312)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1043)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.activated(GraphicalEditorPart.java:870)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.partActivated(LayoutEditor.java:378)
    at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditor.partBroughtToTop(LayoutEditor.java:387)
    at org.eclipse.ui.internal.PartListenerList$2.run(PartListenerList.java:87)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
    at org.eclipse.core.runtime.Platform.run(Platform.java:888)
    at org.eclipse.ui.internal.PartListenerList.fireEvent(PartListenerList.java:57)
    at org.eclipse.ui.internal.PartListenerList.firePartBroughtToTop(PartListenerList.java:85)
    at org.eclipse.ui.internal.PartService.firePartBroughtToTop(PartService.java:208)
    at org.eclipse.ui.internal.WorkbenchPagePartList.firePartBroughtToTop(WorkbenchPagePartList.java:76)
    at org.eclipse.ui.internal.WorkbenchPagePartList.fireActiveEditorChanged(WorkbenchPagePartList.java:52)
    at org.eclipse.ui.internal.PartList.setActiveEditor(PartList.java:162)
    at org.eclipse.ui.internal.WorkbenchPage.makeActiveEditor(WorkbenchPage.java:1281)
    at org.eclipse.ui.internal.WorkbenchPage.setActivePart(WorkbenchPage.java:3530)
    at org.eclipse.ui.internal.WorkbenchPage.requestActivation(WorkbenchPage.java:3077)
    at org.eclipse.ui.internal.PartPane.requestActivation(PartPane.java:279)
    at org.eclipse.ui.internal.EditorPane.requestActivation(EditorPane.java:98)
    at org.eclipse.ui.internal.PartPane.setFocus(PartPane.java:325)
    at org.eclipse.ui.internal.EditorPane.setFocus(EditorPane.java:127)
    at org.eclipse.ui.internal.PartStack.presentationSelectionChanged(PartStack.java:844)
    at org.eclipse.ui.internal.PartStack.access$1(PartStack.java:827)
    at org.eclipse.ui.internal.PartStack$1.selectPart(PartStack.java:137)
    at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation$1.handleEvent(TabbedStackPresentation.java:133)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:269)
    at org.eclipse.ui.internal.presentations.util.AbstractTabFolder.fireEvent(AbstractTabFolder.java:278)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder.access$1(DefaultTabFolder.java:1)
    at org.eclipse.ui.internal.presentations.defaultpresentation.DefaultTabFolder$2.handleEvent(DefaultTabFolder.java:88)
    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:2743)
    at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:1429)
    at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:257)
    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:4066)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
    at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
    at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
    at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
    at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
    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:369)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
    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:620)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
 

我错了什么? 请帮帮我。抱歉我的英语!

2 个答案:

答案 0 :(得分:0)

这可能与你没有声明@Override标签有关。

尝试将代码更改为:

mDateSetListener = DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();

                }
             }; 

答案 1 :(得分:0)

尝试:

postDelayed(new Runnable() {
public void run() {
                dateSetListener = new OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {

                    }
                };
            }
        }, 0);