1

我正在开发一个应用程序。保存/编译我的应用程序时,出现错误

Syntax error, insert ";" to complete Statement

Syntax error, insert ")" to complete Expression

在这里的底部}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}


}

我无法弄清楚为什么我会收到这些错误。我查看了我的代码,但没有发现 open () 也没有需要 ; 的地方。我的代码发布在下面。

Stationlist.java

public class StationList extends Activity {

    Spinner Spinner1 = (Spinner) findViewById(R.id.spinner1); 
    Spinner Spinner2 = (Spinner) findViewById(R.id.spinner2);
    String Red_Line = this.getString(R.string.Red_Line);
    String Blue_Line = this.getString(R.string.Blue_Line);
    String Green_Line = this.getString(R.string.Green_Line);
    String Orange_Line = this.getString(R.string.Orange_Line);
    String Brown_Line = this.getString(R.string.Brown_Line);
    String Pink_Line = this.getString(R.string.Pink_Line);
    String Purple_Line = this.getString(R.string.Purple_Line);
    String Yellow_Line = this.getString(R.string.Yellow_Line);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_station_list);
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_station_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);


    Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
                // TODO Auto-generated method stub

                String selectedValue = arg0.getSelectedItem().toString();
                if(selectedValue.equalsIgnoreCase(Red_Line))
                {
                    ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Red_Line);

                    Spinner2.setAdapter(firstAdapter);//
                }

               if(selectedValue.equalsIgnoreCase(Blue_Line))
               {
                  ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Blue_Line);

                  Spinner2.setAdapter(firstAdapter);
               }
        }

    public void sendTest(View a) {
        Intent Intent9 = new Intent(StationList.this, TestStation.class);
        startActivityForResult(Intent9, 0); 
        setContentView(R.layout.test_station);
    }

    public void onBackPressed(){
        startActivity(new Intent(StationList.this, MainActivity.class));
        finish();
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }

}
}
}

我会很感激你能给我的任何帮助。谢谢您的帮助。

4

5 回答 5

2

将此方法修复为:

Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        String selectedValue = arg0.getSelectedItem().toString();
        if(selectedValue.equalsIgnoreCase(Red_Line))
        {
            ArrayAdapter<String> firstAdapter = new ArrayAdapter<String (StationList.this,R.array.Red_Line);
            Spinner2.setAdapter(firstAdapter);//
        }

        if(selectedValue.equalsIgnoreCase(Blue_Line))
        {
            ArrayAdapter<String> firstAdapter = new ArrayAdapter<String (StationList.this,R.array.Blue_Line);

            Spinner2.setAdapter(firstAdapter);
        }
    }
});

这个方法是这样的:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

最后,删除三个右花括号中的两个:

你的是这样的:

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}


}
}
}

把它变成这样:

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    } // this ones ends this onNothingSelected method

} // this one ends the entire class
于 2013-10-01T00:19:24.877 回答
0
public boolean onOptionsItemSelected

看起来该方法永远不会关闭....或者说没有在正确的位置关闭。另一位评论者还指出您没有用正确的 ); 包装 new OnItemSelectedListener() { 另见他的代码示例。

于 2013-10-01T00:18:44.850 回答
0

试试看,

 public class StationList extends Activity {   
    Spinner Spinner1;  
    Spinner Spinner2; 
    String Red_Line; 
    String Blue_Line; 
    String Green_Line; 
    String Orange_Line; 
    String Brown_Line; 
    String Pink_Line; 
    String Purple_Line; 
    String Yellow_Line;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_station_list);
    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);
Spinner1 = (Spinner) findViewById(R.id.spinner1); 
Spinner2 = (Spinner) findViewById(R.id.spinner2);
Red_Line = this.getString(R.string.Red_Line);
Blue_Line = this.getString(R.string.Blue_Line);
Green_Line = this.getString(R.string.Green_Line);
Orange_Line = this.getString(R.string.Orange_Line);
Brown_Line = this.getString(R.string.Brown_Line);
Pink_Line = this.getString(R.string.Pink_Line);
Purple_Line = this.getString(R.string.Purple_Line);
Yellow_Line = this.getString(R.string.Yellow_Line);
  Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
           @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

                String selectedValue = arg0.getSelectedItem().toString();
                if(selectedValue.equalsIgnoreCase(Red_Line))
                {
                    ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Red_Line);

                    Spinner2.setAdapter(firstAdapter);//
                }

               if(selectedValue.equalsIgnoreCase(Blue_Line))
               {
                  ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Blue_Line);

                  Spinner2.setAdapter(firstAdapter);



               }



        }



});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_station_list, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}



public void sendTest(View a) {
    Intent Intent9 = new Intent(StationList.this, TestStation.class);
    startActivityForResult(Intent9, 0); 
    setContentView(R.layout.test_station);
    }






public void onBackPressed(){

startActivity(new Intent(StationList.this, MainActivity.class));
finish();
}



@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}


}
于 2013-10-01T04:07:05.507 回答
0

缩进问题。尝试以下解决方案

public class StationList extends Activity {

    Spinner Spinner1 = (Spinner) findViewById(R.id.spinner1); 
    Spinner Spinner2 = (Spinner) findViewById(R.id.spinner2);
    String Red_Line = this.getString(R.string.Red_Line);
    String Blue_Line = this.getString(R.string.Blue_Line);
    String Green_Line = this.getString(R.string.Green_Line);
    String Orange_Line = this.getString(R.string.Orange_Line);
    String Brown_Line = this.getString(R.string.Brown_Line);
    String Pink_Line = this.getString(R.string.Pink_Line);
    String Purple_Line = this.getString(R.string.Purple_Line);
    String Yellow_Line = this.getString(R.string.Yellow_Line);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_station_list);
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_station_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back
                //
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
        return super.onOptionsItemSelected(item);
    }

    Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub
            String selectedValue = arg0.getSelectedItem().toString();
            if(selectedValue.equalsIgnoreCase(Red_Line)) {
                ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Red_Line);
                Spinner2.setAdapter(firstAdapter);//
            }

            if(selectedValue.equalsIgnoreCase(Blue_Line)) {
                ArrayAdapter<String> firstAdapter = new ArrayAdapter<String>(StationList.this,R.array.Blue_Line);
                Spinner2.setAdapter(firstAdapter);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

    public void sendTest(View a) {
        Intent Intent9 = new Intent(StationList.this, TestStation.class);
        startActivityForResult(Intent9, 0); 
        setContentView(R.layout.test_station);
    }

    public void onBackPressed() {
        startActivity(new Intent(StationList.this, MainActivity.class));
        finish();
    }
}
于 2013-10-01T04:48:42.783 回答
0

你在使用你的微调器onOptionsItemSelected吗?如果没有,则Spinner1.setOnItemSelectedListener(new OnItemSelectedListener()使用onCreate如下方法:

// Spinner Spinner1 = (Spinner) findViewById(R.id.spinner1);
// Spinner Spinner2 = (Spinner) findViewById(R.id.spinner2);
// String Red_Line = this.getString(R.string.Red_Line);
// String Blue_Line = this.getString(R.string.Blue_Line);
// String Green_Line = this.getString(R.string.Green_Line);
// String Orange_Line = this.getString(R.string.Orange_Line);
// String Brown_Line = this.getString(R.string.Brown_Line);
// String Pink_Line = this.getString(R.string.Pink_Line);
// String Purple_Line = this.getString(R.string.Purple_Line);
// String Yellow_Line = this.getString(R.string.Yellow_Line);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Show the Up button in the action bar.
    getActionBar().setDisplayHomeAsUpEnabled(true);

    Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            String selectedValue = arg0.getSelectedItem().toString();
            if (selectedValue.equalsIgnoreCase(Red_Line)) {
                // ArrayAdapter<String> firstAdapter = new
                // ArrayAdapter<String>(StationList.this,R.array.Red_Line);
                //
                // Spinner2.setAdapter(firstAdapter);//
            }

            if (selectedValue.equalsIgnoreCase(Blue_Line)) {
                // ArrayAdapter<String> firstAdapter = new
                // ArrayAdapter<String>(StationList.this,R.array.Blue_Line);
                //
                // Spinner2.setAdapter(firstAdapter);

            }

        }

        public void sendTest(View a) {
            // Intent Intent9 = new Intent(StationList.this,
            // TestStation.class);
            // startActivityForResult(Intent9, 0);
            // setContentView(R.layout.test_station);
        }

        public void onBackPressed() {

            // startActivity(new Intent(StationList.this,
            // MainActivity.class));
            // finish();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.activity_station_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

于 2013-10-01T05:23:50.037 回答