无法在另一个类中调用方法 - ANDROID

时间:2015-02-21 21:55:58

标签: java android

最近我打开了一个关于如何在另一个类中调用方法的问题...好吧,我确实调用了示例方法,但我真正想调用的方法是不工作:

我有2个班级:

我想在这里调用方法:TelaCadastroRestaurante.java

我在这里有方法:Metodos.java

http://i.imgur.com/JVMjz8J.png

http://i.imgur.com/IuBTmCY.png


公共类TelaCadastroRestaurante扩展了活动{

private EditText nomeRestaurante, emailRestaurante, telefoneRestaurante;
private Button buttonProximo;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tela_cadastro_restaurante);
    incializarComponentes();
    acaoBotoes();
}

public void incializarComponentes() {
    nomeRestaurante = (EditText) findViewById(R.id.editTextNomeRestauranteTelaCadastroRestaurante);
    emailRestaurante = (EditText) findViewById(R.id.editTextEmailRestauranteTelaCadastroRestaurante);
    telefoneRestaurante = (EditText) findViewById(R.id.editTextTelefoneRestauranteTelaCadastroRestaurante);
    buttonProximo = (Button) findViewById(R.id.buttonProximoTelaCadastroRestaurante);

}

public void acaoBotoes() {
    buttonProximo.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            Metodos metodos = new Metodos();
            Metodos.taskInProgres(true, getApplicationContext());       
        }
    });
}

public void pegarValores(){

    Restaurante rest = new Restaurante();

    rest.setNomeRest(nomeRestaurante.getText().toString());
    rest.setEmailRest(emailRestaurante.getText().toString());
    rest.setTelefoneRest(Integer.parseInt(telefoneRestaurante.getText().toString()));

    Toast.makeText(getApplicationContext(), rest.getNomeRest() + "\n" +  rest.getEmailRest() + "\n" + rest.getTelefoneRest(), Toast.LENGTH_SHORT).show();
}

}

CLASS WITH METHODS

公共课堂Metodos {

private static ProgressDialog dialog;

public static void taskInProgres(boolean mostrar, Context context) {

    if (dialog == null) {
        dialog = new ProgressDialog(context);
        dialog = ProgressDialog.show(context, "","Aguarde a verificação...", true);
    }
    if (mostrar) {
        dialog.show();
    } else {
        dialog.dismiss();
    }
}

}

2 个答案:

答案 0 :(得分:0)

xMetodos.java班级中,将方法更改为静态:

private static void...TelaCadastroRestaurante.java类称之为:

xMetodos.taskInProgres(true,getApplicationContext());

如果您将方法声明为static,则可以在不实例化的情况下调用它。

答案 1 :(得分:0)

您必须创建class xMethodos的实例才能调用该方法。

代码如下:

xMethodos foo = new xMethodos ():
foo.taskInProgress([YOUR_BOOL], [YOUR_CONTEXT);

上下文可能是您的活动,可以称为this