2015-02-23 33 views
-1

請幫助我...我不知道我在做什麼錯!Android中的吸氣器和安裝器

我有3個班。

1 - 活性= TelaCadastroRestaurant.java

2 - 實體= Restaurant.java

3 - 方法的類= Metodos.java

問題:當我打電話getNomeRestaurante()在方法callMandarNuvem()ANY其他方法但是pegarvalores()它不起作用 ...它顯示爲空。

錯誤的打印:http://i.imgur.com/sOcBkUM.png

********************實例***************** **********

如果我創建一個餐廳對象INSIDE方法pegarValores(),並在一個吐司,調用:getNomeRestaurante()...它是所有正常。但是,如果我在方法pegarValores()和Toast中創建一個Restaurant對象,請調用:getNomeRestaurante()... IT SHOWS NULL。

*********************方法的意義*******************

方法 - > inicializaComponentes():它引用了Activity的組件。

方法 - > acaoBotoes():它處理上的按鈕的點擊

方法 - > pegarValores():這意味着,捕值。

方法 - > mandarNuvem():這意味着,發送到clound(保存)

方法 - > caixaCerteza():它的意思,你確定你想做的事是什麼?

方法 - > taskInProgress():它的意思是,加載..等待


公共類TelaCadastroRestaurante延伸活動{

private Button proximoButton; 
private EditText nomeRestauranteEditText, emailRestauranteEditText, telefoneRestauranteEditText; 

private String nomeRestauranteValores, emailRestauranteValores; 
private int telefoneRestauranteValores; 

private String voceTemCerteza = "Você tem certeza que deseja cadastrar o restaurante "; 


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

public void incializarComponentes() { 
    nomeRestauranteEditText = (EditText) findViewById(R.id.editTextNomeRestauranteTelaCadastroRestaurante); 
    emailRestauranteEditText = (EditText) findViewById(R.id.editTextEmailRestauranteTelaCadastroRestaurante); 
    telefoneRestauranteEditText = (EditText) findViewById(R.id.editTextTelefoneRestauranteTelaCadastroRestaurante); 
    proximoButton = (Button) findViewById(R.id.buttonProximoTelaCadastroRestaurante); 
} 

public void acaoBotoes() { 
    proximoButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      pegarValores(); 
      callMandarNuvem(); 
     } 
    }); 
} 

public void pegarValores(){ 
    Restaurante rest = new Restaurante(); 

    nomeRestauranteValores = nomeRestauranteEditText.getText().toString(); 
    emailRestauranteValores = emailRestauranteEditText.getText().toString(); 
    telefoneRestauranteValores = Integer.parseInt(telefoneRestauranteEditText.getText().toString()); 

    rest.setNomeRest(nomeRestauranteValores); 
    rest.setEmailRest(emailRestauranteValores); 
    rest.setTelefoneRest(telefoneRestauranteValores); 

} 

public void callMandarNuvem(){ 
    Runnable r = new Runnable() { 
     public void run() { 
      Metodos.mandarNuvem(TelaCadastroRestaurante.this); 
     } 
    }; 

    Restaurante rest = new Restaurante(); 
    Metodos.caixaCerteza(TelaCadastroRestaurante.this, voceTemCerteza + rest.getNomeRest() + "?",r); 

} 

}


公共類餐廳{

private String idRest; 
private String nomeRest; 
private String emailRest; 
private int telefoneRest; 

public Restaurante() { 
} 

public Restaurante(String nomeRest, String emailRest, int telefoneRest) { 
    this.nomeRest = nomeRest; 
    this.emailRest = emailRest; 
    this.telefoneRest = telefoneRest; 
} 

public String getIdRest() { 
    return idRest; 
} 

public void setIdRest(String idRest) { 
    this.idRest = idRest; 
} 

public String getNomeRest() { 
    return nomeRest; 
} 

public void setNomeRest(String nomeRest) { 
    this.nomeRest = nomeRest; 
} 

public String getEmailRest() { 
    return emailRest; 
} 

public void setEmailRest(String emailRest) { 
    this.emailRest = emailRest; 
} 

public int getTelefoneRest() { 
    return telefoneRest; 
} 

public void setTelefoneRest(int telefoneRest) { 
    this.telefoneRest = telefoneRest; 
} 

@Override 
public String toString() { 
    return nomeRest; 
} 

}


公共類Metodos {

private static ProgressDialog dialog; 

// Metodo que mostra o Aguarde a verificação 
public static void taskInProgres(boolean mostrar, Context context) { 

    if (dialog == null) { 
     dialog = new ProgressDialog(context); 
     dialog = ProgressDialog.show(context, "","Espere um momento...", true); 
    } 
    if (mostrar) { 
     dialog.show(); 
    } else { 
     dialog.dismiss(); 
    } 
} 

// Metodo que mostra a caixa de certeza 
public static void caixaCerteza(final Context context, final String texto, final Runnable func) { 
    AlertDialog.Builder builderaction = new AlertDialog.Builder(context); 
    builderaction.setTitle("Atenção!"); 
    builderaction.setMessage(texto); 

    builderaction.setPositiveButton("Sim",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        func.run(); 
       } 
      }); 
    builderaction.setNegativeButton("Não",new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
       } 
      }); 
    AlertDialog alert = builderaction.create(); 
    alert.setIcon(R.drawable.ic_stop); 
    alert.show(); 
} 

// Metodo que manda pra nuvem 
public static void mandarNuvem(final Context context){ 

    Metodos.taskInProgres(true, context); 

     Restaurante rest = new Restaurante(); 

     ParseObject restauranteParse = new ParseObject("Restaurante"); 
     restauranteParse.put("nomeRestaurante", rest.getNomeRest()); 
     restauranteParse.put("emailRestaurante", rest.getEmailRest()); 
     restauranteParse.put("telefoneRestaurante", rest.getTelefoneRest()); 
     restauranteParse.saveInBackground(new SaveCallback() { 
      @Override 
      public void done(ParseException e) { 
       if (e == null) { 
        Toast.makeText(context,"Salvo com sucesso!", Toast.LENGTH_SHORT).show(); 
        Metodos.taskInProgres(false, context); 
       } else { 
        Toast.makeText(context, e.getMessage(),Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
} 

}

+0

請更正代碼在您的文章格式。你能問一個明確的問題嗎? – kenorb 2015-02-23 21:51:37

+0

@kenorb,我很抱歉,但我不知道如何更具體......我該如何糾正我的帖子?它出什麼問題了? – lurdes 2015-02-23 22:01:53

+0

Android中的獲取器和設置器像普通Java一樣工作,沒有什麼區別。如果他們返回'null',那麼這是在類上設置的值。 – m0skit0 2015-02-23 22:49:20

回答

0

你正在創建的Restaurant一個新實例,不設置任何值。我認爲這是你在找什麼:

public class TelaCadastroRestaurante extends Activity { 

    private Button proximoButton; 
    private EditText nomeRestauranteEditText, emailRestauranteEditText, telefoneRestauranteEditText; 

    private String nomeRestauranteValores, emailRestauranteValores; 
    private int telefoneRestauranteValores; 

    private String voceTemCerteza = "Você tem certeza que deseja cadastrar o restaurante "; 

    private final Restaurante rest; 


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

    public void incializarComponentes() { 
    nomeRestauranteEditText = (EditText)     findViewById(R.id.editTextNomeRestauranteTelaCadastroRestaurante); 
    emailRestauranteEditText = (EditText) findViewById(R.id.editTextEmailRestauranteTelaCadastroRestaurante); 
     telefoneRestauranteEditText = (EditText) findViewById(R.id.editTextTelefoneRestauranteTelaCadastroRestaurante); 
     proximoButton = (Button) findViewById(R.id.buttonProximoTelaCadastroRestaurante); 
    } 

    public void acaoBotoes() { 
    proximoButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     pegarValores(); 
     callMandarNuvem(); 
    } 
}); 
} 

    public void pegarValores(){ 
    rest = new Restaurante(); 

    nomeRestauranteValores =  nomeRestauranteEditText.getText().toString(); 
    emailRestauranteValores = emailRestauranteEditText.getText().toString(); 
    telefoneRestauranteValores = Integer.parseInt(telefoneRestauranteEditText.getText().toString()); 

    rest.setNomeRest(nomeRestauranteValores); 
    rest.setEmailRest(emailRestauranteValores); 
    rest.setTelefoneRest(telefoneRestauranteValores); 

    } 

    public void callMandarNuvem(){ 
    Runnable r = new Runnable() { 
    public void run() { 
     Metodos.mandarNuvem(TelaCadastroRestaurante.this); 
    } 
}; 

    Metodos.caixaCerteza(TelaCadastroRestaurante.this, voceTemCerteza + rest.getNomeRest() + "?",r); 

}

在你Metodos類,改變mandarNuvem到:

// Metodo que manda pra nuvem 
public static void mandarNuvem(final Context context,Restaurante rest){ 

Metodos.taskInProgres(true, context); 

    ParseObject restauranteParse = new ParseObject("Restaurante"); 
    restauranteParse.put("nomeRestaurante", rest.getNomeRest()); 
    restauranteParse.put("emailRestaurante", rest.getEmailRest()); 
    restauranteParse.put("telefoneRestaurante", rest.getTelefoneRest()); 
    restauranteParse.saveInBackground(new SaveCallback() { 
     @Override 
     public void done(ParseException e) { 
      if (e == null) { 
       Toast.makeText(context,"Salvo com sucesso!", Toast.LENGTH_SHORT).show(); 
       Metodos.taskInProgres(false, context); 
      } else { 
       Toast.makeText(context, e.getMessage(),Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 
} 

然後你的電話改變了方法:

public void callMandarNuvem(){ 
Runnable r = new Runnable() { 
    public void run() { 
     Metodos.mandarNuvem(TelaCadastroRestaurante.this,rest); 
    } 
};  
+0

我需要在類metodos中創建一個外部餐廳對象嗎? – lurdes 2015-02-23 22:34:31

+0

不明白爲什麼,因爲在'Metodos'中你只能在'mandarNuvem'方法中使用它。 – vinitius 2015-02-23 22:36:51

+0

是的,我同意你的意見。 但是,它差不多完成了..:D 我只有這個錯誤: http://i.imgur.com/CV8oewg.png – lurdes 2015-02-23 22:37:14

0
Restaurante rest = new Restaurante(); 
Metodos.caixaCerteza(TelaCadastroRestaurante.this, voceTemCerteza + rest.getNomeRest() + "?",r); 

您所呼叫的實體級的默認構造函數建立一個 「空」 的餐廳。所以String nomeRest保持它在那個時刻爲空的值。

因此而叫,你通過你的價值觀其他構造或者就像你在其他的方法確實使用了setter方法!

+0

我明白了你的意思,但問題是:我要去要一直使用getNomeRest和其他getter,並且如果我使用getter和setter,它會變得更容易.... – lurdes 2015-02-23 22:42:08