“Cannot make a static reference to a non-static method” in Android

前端 未结 2 1212
天命终不由人
天命终不由人 2020-12-19 09:56

I\'m having some issues with the old \"Cannot make a static reference to a non-static method\" error in my Android program. I am creating a sand falling game (similar to the

相关标签:
2条回答
  • 2020-12-19 10:01

    You have to call showDialog on a DemoActivity instance, NOT on the class itself. The only time you can call ClassName.methodName() is if the method is defined as static. showDialog is not a static method.

    To fix this, you either need to instantiate a new DemoActivity or get an existing one, then call showDialog on that.

    Edit: If you already have a DemoActivity instance when you instantiate this Control object, perhaps the following modification will work:

    public class Control extends LinearLayout
    {
    
        ...
    
        // add an Activity instance
        private Activity activity;
    
        // set the Activity in your constructor
        public Control(Context context, AttributeSet attrs, Activity activity)
        {
            super(context, attrs);
            this.activity = activity;
        }
    
        @Override
        protected void onFinishInflate()
        {
            ...
    
               // Use the instance activity here
               activity.showDialog(2);
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-19 10:18

    if the create is called by ANDROID, so you do not create the instance, just put into the create mShowDialog=this or mShowDialog=pShowDialog

    in other words - have the create save the instance value also you can add a public get to get that instance value. Then you can access the instance function through the abstract by interceding the getter:

    ABSTRACTCLASS.getInstance().applyFunction();
    
    0 讨论(0)
提交回复
热议问题