2017-01-26 11 views
-1

Я новичок в студии android и попробовал учебник, где ведущий продемонстрировал свою программу, но когда я набираю код, я постоянно получаю ошибку, например Не удается разрешить Symbol setOnClickListener, где setOnClickListener выделено красным цветом.Где я ошибаюсь setOnClickListener всегда в красном

TextView Resultant = (TextView) findViewById(R.id.Resultant); 
EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input); 
EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input); 

@Override 
public View findViewById(@IdRes int id) { 
    return super.findViewById(R.id.Percentage_Input); 
} 

Button calc_btn = (Button) findViewById(R.id.calc_btn); 
calc_btn.setOnClickListener(new View.OnClickListener(){ 

    public void onClick((View) view); { 

     float percent = Float.parseFloat(Percentage_Input.getText().toString()); 
     float dec = percent/100; 
     float Qty = Float.parseFloat(Number_Input.getText().toString()); 
     float total = dec*Qty; 
     Resultant.setText(Float.toString(total)); 


    } 
}) 

ответ

0

Переместить; и поставьте в конце:

TextView Resultant = (TextView) findViewById(R.id.Resultant); 
EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input); 
EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input); 

@Override 
public View findViewById(@IdRes int id) { 
    return super.findViewById(R.id.Percentage_Input); 
} 

Button calc_btn = (Button) findViewById(R.id.calc_btn); 
calc_btn.setOnClickListener(new View.OnClickListener(){ 

    public void onClick((View) view) { 

     float percent = Float.parseFloat(Percentage_Input.getText().toString()); 
     float dec = percent/100; 
     float Qty = Float.parseFloat(Number_Input.getText().toString()); 
     float total = dec*Qty; 
     Resultant.setText(Float.toString(total)); 


    } 
}); 
0

Я думаю, что код не в нужном месте. Я вижу, что вы переопределяете findViewById (интересно, почему), а остальная часть вашего кода также находится на том же уровне. Вы должны поместить весь этот код в метод жизненного цикла.

Может быть, в onCreate()

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    TextView Resultant = (TextView) findViewById(R.id.Resultant); 
    EditText Percentage_Input = (EditText) findViewById(R.id.Percentage_Input); 
    EditText Number_Input = (EditText) findViewById(R.id.Percentage_Input); 
    Button calc_btn = (Button) findViewById(R.id.calc_btn); 
    calc_btn.setOnClickListener(new View.OnClickListener() { 

     public void onClick((View) view); { 

      float percent = Float.parseFloat(Percentage_Input.getText().toString()); 
      float dec = percent/100; 
      float Qty = Float.parseFloat(Number_Input.getText().toString()); 
      float total = dec * Qty; 
      Resultant.setText(Float.toString(total)); 

     } 
    }); 
} 

Также отсутствует точка с запятой ; в конце установки OnClickListener.

0

Удалите метод findViewById, это какой-то странный материал, возвращающий всегда тот же вид.

Вставьте остальную часть кода внутри в поле Создать и добавьте отсутствующий; в конце.

Смежные вопросы