如何在Android中的onCreate()内创建一个方法[英] How to create a method inside onCreate() in Android

问题描述

如何在onCreate()方法中创建方法?当我创建其显示错误时:

令牌void @预期的语法错误

和如果无法在onCreate()方法内创建方法,请告诉我如何在onCreate()中创建一个方法,然后从onCreate()方法中传递mContext和mActivity.

推荐答案

public class MainActivity extends Activity {

//Declare a class variable to use in this class
public <data-type> mContext;
public <data-type> mActivity;


@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = <Your value>;
        mActivity = <Your value>;

        myMethod (mContext , mActivity);

}
private void myMethod (<data-type> mContext, <data-type> mActivity) {
   /*
    Your Code Goes Here
  */
}
}

*请注意,应该用实际类型替换的标签,而不不等式迹象,例如"int"和"short".

其他推荐答案

public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    yourmethod();
}

public void yourmethod(){
// your code here
}

}

本文地址:https://www.itbaoku.cn/post/978690.html