我正在开发一款应用。保存/编译我的应用程序时,我收到错误
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
}
}
}
}
我很感激你能给我的任何帮助。谢谢你的帮助。
答案 0 :(得分: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
答案 1 :(得分:0)
public boolean onOptionsItemSelected
看起来该方法永远不会关闭....或者不是在正确的位置关闭。另一位评论者也指出你没有用新的OnItemSelectedListener(){包装正确的);看他的代码示例。
答案 2 :(得分: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
}
}
答案 3 :(得分: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();
}
}
答案 4 :(得分: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);
}
}