1

アプリを開発しています。アプリを保存/コンパイルすると、エラーが発生します

Syntax error, insert ";" to complete Statement

Syntax error, insert ")" to complete Expression

ここの一番下}に:

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


}

これらのエラーが発生する理由がわかりません。私は自分のコードを調べましたが、開いている () も ; を必要とする場所も見つかりませんでした。私のコードは以下に掲載されています。

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);
}

最後に、3 つの右中括弧のうち 2 つを削除します。

あなたはこれでした:

@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 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 に答える
0
public boolean onOptionsItemSelected

メソッドが閉じられていないように見えます....というか、正しい場所で閉じられていません。また、他のコメンターは、 new OnItemSelectedListener() { を適切な ); でラップしなかったことを指摘しています。彼のコード例も参照してください。

于 2013-10-01T00:18:44.850 に答える