If语句中的android java语法错误

时间:2011-07-19 16:47:39

标签: java android

我的apk给了我两个错误,从今天早上26点开始,但我似乎没有掌握解决方案。第一个问题是有一个很好的方法/技巧来跟踪开放和关闭的parens以及开放和封闭的括号吗?第二个问题我的代码是否存在根本性问题?第一个错误是在私人Button btnExecute;我想我错过了一个封闭的支架,有些地方但不知道在哪里..当昨晚工作时我没有靠近那条线的封闭支架 第二个错误是关闭括号两行从结束onClick。我有一种感觉,它缺少一个分号,paren或支架。 目标是读取微调器中显示的值并使用if语句来确定要运行的例程。在每个例程中,我将检查某些EditText是否为空或空白,如果不是空白则是基于微调器条目的特定公式。

package com.androidbook.triviaquiz8;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class QuizHelpActivity extends QuizActivity {

private EditText m1_sqs1;
private EditText m1_rs1;
private EditText m1_rs2;
private EditText m1_cd;
private EditText m1_els1;
private EditText m1_els2;

private EditText m1_sqs1_eng;
private EditText m1_rs1_eng;
private EditText m1_rs2_eng;
private EditText m1_cd_eng;
private EditText m1_els1_eng;
private EditText m1_els2_eng;

private EditText m1_sqs1_met;
private EditText m1_rs1_met;
private EditText m1_rs2_met;
private EditText m1_cd_met;
private EditText m1_els1_met;
private EditText m1_els2_met;

private EditText m1_sa_in;
private EditText m1_sa_ft;
private EditText m1_sa_mm;
private EditText m1_sa_m;

private EditText m1_ed_in;
private EditText m1_ed_ft;
private EditText m1_ed_mm;
private EditText m1_ed_m;

private Spinner m1_ss_spinner;
private Spinner m1_sqs1_spinner;
private Spinner m1_rs1_spinner;
private Spinner m1_rs2_spinner;
private Spinner m1_cd_spinner;
private Spinner m1_els1_spinner;
private Spinner m1_els2_spinner;

private QuizHelpActivity mContext;

private Button btnSave;
private Button btnClear;
private Button btnExecute;


// 
// onCreate - let the fun begin!
// 

@Override 
    public void onCreate(Bundle savedInstanceState){      
        super.onCreate(savedInstanceState); 
        mContext = this; 
        setContentView(R.layout.help);

        m1_sqs1 = (EditText) findViewById(R.id.m1_sqs1);
        m1_rs1  = (EditText) findViewById(R.id.m1_rs1);
        m1_rs2  = (EditText) findViewById(R.id.m1_rs2);
        m1_cd   = (EditText) findViewById(R.id.m1_cd);
        m1_els1 = (EditText) findViewById(R.id.m1_els1);
        m1_els2 = (EditText) findViewById(R.id.m1_els2);

        m1_sqs1_eng = (EditText) findViewById(R.id.m1_sqs1);
        m1_rs1_eng  = (EditText) findViewById(R.id.m1_rs1);
        m1_rs2_eng  = (EditText) findViewById(R.id.m1_rs2);
        m1_cd_eng   = (EditText) findViewById(R.id.m1_cd);
        m1_els1_eng = (EditText) findViewById(R.id.m1_els1);
        m1_els2_eng = (EditText) findViewById(R.id.m1_els2);

        m1_sqs1_met = (EditText) findViewById(R.id.m1_sqs1);
        m1_rs1_met  = (EditText) findViewById(R.id.m1_rs1);
        m1_rs2_met  = (EditText) findViewById(R.id.m1_rs2);
        m1_cd_met   = (EditText) findViewById(R.id.m1_cd);
        m1_els1_met = (EditText) findViewById(R.id.m1_els1);
        m1_els2_met = (EditText) findViewById(R.id.m1_els2);

        m1_sa_in    = (EditText) findViewById(R.id.m1_sa_in);
        m1_sa_ft    = (EditText) findViewById(R.id.m1_sa_ft);
        m1_sa_mm    = (EditText) findViewById(R.id.m1_sa_mm);
        m1_sa_m     = (EditText) findViewById(R.id.m1_sa_m);

        m1_ed_in    = (EditText) findViewById(R.id.m1_ed_in);
        m1_ed_ft    = (EditText) findViewById(R.id.m1_ed_ft);
        m1_ed_mm    = (EditText) findViewById(R.id.m1_ed_mm);
        m1_ed_m     = (EditText) findViewById(R.id.m1_ed_m);


        //
        // Create an OnClick Event in each button.
        //

        Button btnExecute   = (Button) findViewById(R.id.btnExecute);
        Button btnSave      = (Button) findViewById(R.id.btnSave);
        Button btnClear     = (Button) findViewById(R.id.btnClear);

        btnSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                }
            });
        btnClear.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                }
            });
        btnExecute.setOnClickListener(new View.OnClickListener() {
            private AlertDialog show;
            public void onClick(View arg0) {
                AdapterView<SpinnerAdapter> m1ssspinner;
                if (((m1ssspinner.getSelectedItem() == "Square"))) {
                            if ((m1_sqs1.getText().length() == 0)           
                            || (m1_sqs1.getText().toString() == " "))   {           
                                show = new AlertDialog.Builder(mContext).setTitle("Error")
                                .setMessage("The Square Side length is empty")               
                                .setPositiveButton("OK", null).show();          
                            } else {
                                double result = new Double(m1_sqs1.getText().toString()) * new Double(m1_sqs1.getText().toString());   
                                m1_sa_in.setText(Double.toString(result));
                                    } 
                        if (((m1ssspinner.getSelectedItem() == "Rectangle"))) {
                            if ((m1_rs1.getText().length() == 0)           
                            || (m1_rs1.getText().toString() == " ")
                            || (m1_rs1.getText().toString() == " ")
                            || (m1_rs1.getText().length() == 0)){
                                show = new AlertDialog.Builder(mContext).setTitle("Error")
                                .setMessage("A Rectangle Side length is empty")               
                                .setPositiveButton("OK", null).show();
                            } else {
                                double result = new Double(m1_rs1.getText().toString()) * new Double(m1_rs2.getText().toString());   
                                m1_sa_in.setText(Double.toString(result));
                                    } 
                        if (((m1ssspinner.getSelectedItem() == "Circle"))) {
                            if ((m1_cd.getText().length() == 0)           
                            || (m1_cd.getText().toString() == " ") 
                            || (m1_cd.getText().toString() == " ")){
                                show = new AlertDialog.Builder(mContext).setTitle("Error")
                                .setMessage("The Circle Diameter is empty")               
                                .setPositiveButton("OK", null).show();
                            } else {
                                double result = new Double(m1_cd.getText().toString()) * new Double(m1_cd.getText().toString());   
                                m1_sa_in.setText(Double.toString(result));
                                    } 
                        if (((m1ssspinner.getSelectedItem() == "Elliptical"))) {
                            if ((m1_els1.getText().length() == 0)           
                            || (m1_els1.getText().toString() == " ")
                            || (m1_els2.getText().toString() == 0)
                            || (m1_els2.getText().toString() == " ")){
                                show = new AlertDialog.Builder(mContext).setTitle("Error")
                                .setMessage("An Elliptical Diameter is empty")               
                                .setPositiveButton("OK", null).show();
                            } else {
                                double result = new Double(m1_els1.getText().toString()) * new Double(m1_els2.getText().toString());   
                                m1_sa_in.setText(Double.toString(result));
                            }
        );

//
// End OnClick Event in each button.
//

// 
// check media availability
//

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) { 

// We can read and write the media    

mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {    

// We can only read the media    

mExternalStorageAvailable = true;    
mExternalStorageWriteable = false;} else {    

// Something else is wrong. It may be one of many other states, but all we need    
//  to know is we can neither read nor write    

mExternalStorageAvailable = mExternalStorageWriteable = false;       
}    

// 
// end check media availability
//           

// 
// create METHOD 1 STACK SHAPE SPINNER  (m1_ss_spinner) with displayed text 'centered'
// 

Spinner m1ssspinner = (Spinner)findViewById(R.id.m1_ss_spinner);        
    ArrayAdapter<CharSequence> m1ssadapter = ArrayAdapter.createFromResource(this, R.array.m1_ss_list, R.layout.my_normal_spinner_style);            
    m1ssadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    m1ssspinner.setAdapter(m1ssadapter); 

// 
// get METHOD 1 STACK SHAPE UNIT  (m1_ss_unit)
// 

    Object m1_ss_unit = (m1ssspinner.getSelectedItem());

// 
// create METHOD 1 SQUARE_SIDE1 SPINNER  (m1_sqs1_spinner) with displayed text 'centered'
// 

    Spinner m1sqs1spinner = (Spinner)findViewById(R.id.m1_sqs1_spinner);        
        ArrayAdapter<CharSequence> m1sqs1adapter = ArrayAdapter.createFromResource(this, R.array.m1_sqs1_list, R.layout.my_normal_spinner_style);            
        m1sqs1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1sqs1spinner.setAdapter(m1sqs1adapter); 

// 
// create METHOD 1 RECTANGLE_SIDE1 SPINNER  (m1_rs1_spinner) with displayed text 'centered'
// 

    Spinner m1rs1spinner = (Spinner)findViewById(R.id.m1_rs1_spinner);        
        ArrayAdapter<CharSequence> m1rs1adapter = ArrayAdapter.createFromResource(this, R.array.m1_rs1_list, R.layout.my_normal_spinner_style);            
        m1rs1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1rs1spinner.setAdapter(m1rs1adapter);

// 
// create METHOD 1 RECTANGLE_SIDE2 SPINNER  (m1_rs2_spinner) with displayed text 'centered'
// 

    Spinner m1rs2spinner = (Spinner)findViewById(R.id.m1_rs2_spinner);        
        ArrayAdapter<CharSequence> m1rs2adapter = ArrayAdapter.createFromResource(this, R.array.m1_rs2_list, R.layout.my_normal_spinner_style);            
        m1rs2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1rs2spinner.setAdapter(m1rs2adapter);      

// 
// create METHOD 1 CIRCULAR DIAMETER SPINNER  (m1_cd_spinner) with displayed text 'centered'
// 

    Spinner m1cdspinner = (Spinner)findViewById(R.id.m1_cd_spinner);        
        ArrayAdapter<CharSequence> m1cdadapter = ArrayAdapter.createFromResource(this, R.array.m1_cd_list, R.layout.my_normal_spinner_style);            
        m1cdadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1cdspinner.setAdapter(m1cdadapter);

// 
// create METHOD 1 ELLIPTICAL DIAMETER1 SPINNER  (m1_cd_spinner) with displayed text 'centered'
// 

    Spinner m1els1spinner = (Spinner)findViewById(R.id.m1_els1_spinner);        
        ArrayAdapter<CharSequence> m1els1adapter = ArrayAdapter.createFromResource(this, R.array.m1_els1_list, R.layout.my_normal_spinner_style);            
        m1els1adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1els1spinner.setAdapter(m1els1adapter);

// 
// create METHOD 1 ELLIPTICAL DIAMETER2 SPINNER  (m1_cd_spinner) with displayed text 'centered'
// 

    Spinner m1els2spinner = (Spinner)findViewById(R.id.m1_els2_spinner);        
        ArrayAdapter<CharSequence> m1els2adapter = ArrayAdapter.createFromResource(this, R.array.m1_els2_list, R.layout.my_normal_spinner_style);            
        m1els2adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            
        m1els2spinner.setAdapter(m1cdadapter);          

}

5 个答案:

答案 0 :(得分:2)

这里有很多问题,我甚至不知道从哪里开始。这当然不会编译,因为你有不平衡的{},但这不仅仅是你的问题

你的第一个if / then包括所有其他的并阻止任何其他人解析为真如

if (((m1ssspinner.getSelectedItem() == "Square")))

封装所有其他测试,例如下一个

if (((m1ssspinner.getSelectedItem() == "Rectangle"))) 

如果第一个是真的,那么第二个永远不会。

您还有不同类型的本地和类范围的变量,这些变量具有相同的名称,并且本地未初始化(或者没有正确的类型)

AdapterView<SpinnerAdapter> m1ssspinner; // Probably not what you want to use ... 

Spinner m1ssspinner = (Spinner) ...

然后你有重复测试的块

if ((m1_rs1.getText().length() == 0)           // Same test twice! 
    || (m1_rs1.getText().toString() == " ")
    || (m1_rs1.getText().toString() == " ")
    || (m1_rs1.getText().length() == 0))

或者你正在做同样的事情,还要测试一个字符串对int

 if (((m1ssspinner.getSelectedItem() == "Elliptical"))) {
    if ((m1_els1.getText().length() == 0)           
    || (m1_els1.getText().toString() == " ")
    || (m1_els2.getText().toString() == 0)       // you probably meant int!
    || (m1_els2.getText().toString() == " ")){

此外,您的测试是将从微调器返回的对象与字符串文字(它们不匹配)进行比较

您正在初级化您的视图类,而不是在setcontent视图等之后的oncreate等等。

无论如何这个块至少会编译,但是这个程序根本不会像写的那样工作,你还有很多东西可以解决。

我的建议是一步一步,如果你发现你有老鼠窝,如果/那你可能需要重新考虑解决方案

btnExecute.setOnClickListener(new View.OnClickListener() 
{
     private AlertDialog show;
     public void onClick(View arg0) 
     {
         //AdapterView<SpinnerAdapter> m1ssspinner; // Probably you meant to use the                  class scoped of same name  

         if (((m1ssspinner.getSelectedItem() == "Square"))) {
             if ((m1_sqs1.getText().length() == 0)           
                 || (m1_sqs1.getText().toString() == " ")) {           
                     show = new AlertDialog.Builder(mContext).setTitle("Error")
                        .setMessage("The Square Side length is empty")               
                        .setPositiveButton("OK", null).show();          
                    } else 
                    {
                        double result = new Double(m1_sqs1.getText().toString()) * new                             Double(m1_sqs1.getText().toString());   
                        m1_sa_in.setText(Double.toString(result));
                    } 
     } else      

     if (((m1ssspinner.getSelectedItem() == "Rectangle"))) {
            if ((m1_rs1.getText().length() == 0)           
            || (m1_rs1.getText().toString() == " ")
            || (m1_rs1.getText().toString() == " ")
            || (m1_rs1.getText().length() == 0))           {
            show = new AlertDialog.Builder(mContext).setTitle("Error")
            .setMessage("A Rectangle Side length is empty")               
            .setPositiveButton("OK", null).show();
            } else 
            {
                 double result = new Double(m1_rs1.getText().toString()) * new                           Double(m1_rs2.getText().toString());   
            m1_sa_in.setText(Double.toString(result));
            } 
     } else       

     if (((m1ssspinner.getSelectedItem() == "Circle"))) {
            if ((m1_cd.getText().length() == 0)           
                 || (m1_cd.getText().toString() == " ") 
                 || (m1_cd.getText().toString() == " "))
            {
                show = new AlertDialog.Builder(mContext).setTitle("Error")
                    .setMessage("The Circle Diameter is empty")               
                    .setPositiveButton("OK", null).show();
            } else 
            {
                 double result = new Double(m1_cd.getText().toString()) * new Double(m1_cd.getText().toString());   
                         m1_sa_in.setText(Double.toString(result));
            }
      } else
      if ((m1ssspinner.getSelectedItem() == "Elliptical")) {
                    if ((m1_els1.getText().length() == 0)           
                    || (m1_els1.getText().toString() == " ")
                    || (m1_els2.getText().length() == 0)
                    || (m1_els2.getText().toString() == " "))
                    {
                        show = new AlertDialog.Builder(mContext).setTitle("Error")
                        .setMessage("An Elliptical Diameter is empty")               
                        .setPositiveButton("OK", null).show();
                    } else {
                        double result = new Double(m1_els1.getText().toString()) * new Double(m1_els2.getText().toString());   
                        m1_sa_in.setText(Double.toString(result));
                    }
                }

            }   
        }

        );

答案 1 :(得分:0)

下面似乎错了。将AlertDialog显示在函数

btnExecute.setOnClickListener(new View.OnClickListener() 
{             

private AlertDialog show;
public void onClick(View arg0)

答案 2 :(得分:0)

带括号的错误。我建议你通过一个数字来设置评论。

如果您打开括号,请添加1,如果您有一个括号,则减1。

这将有助于您将来克服这个问题:

if(true)
{ //1
    while(true)
    { //2
    } //1

} //0

答案 3 :(得分:0)

问题在你的btnExecute.you没有放'}'。在btnExecute之后看到最后的其他你要放另一个}

答案 4 :(得分:0)

以下没有结束括号......

btnExecute.setOnClickListener(new View.OnClickListener() { ... 
    public void onClick(View arg0) { ...
        if (((m1ssspinner.getSelectedItem() == "Square"))) { ...
            if (((m1ssspinner.getSelectedItem() == "Rectangle"))) { ...            
            if (((m1ssspinner.getSelectedItem() == "Circle"))) { ...
            if (((m1ssspinner.getSelectedItem() == "Elliptical"))) { ...

您的btnExecute.setOnClickListener应如下所示

btnExecute.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
            ... your if statements ... 
    }
});