当我点击我的按钮时,我的应用程序崩溃了。在log cat中它表示我不能解析“”(没有)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
ocjena1 = (EditText) findViewById(R.id.ocjena1);
ocjena2 = (EditText) findViewById(R.id.ocjena2);
ocjena3 = (EditText) findViewById(R.id.ocjena3);
ocjena4 = (EditText) findViewById(R.id.ocjena4);
ocjena5 = (EditText) findViewById(R.id.ocjena5);
ocjena6 = (EditText) findViewById(R.id.ocjena6);
ocjena7 = (EditText) findViewById(R.id.ocjena7);
ocjena8 = (EditText) findViewById(R.id.ocjena8);
ocjena9 = (EditText) findViewById(R.id.ocjena9);
ocjena10 = (EditText) findViewById(R.id.ocjena10);
ocjena11 = (EditText) findViewById(R.id.ocjena11);
ocjena12 = (EditText) findViewById(R.id.ocjena12);
ocjena13 = (EditText) findViewById(R.id.ocjena13);
ocjena14 = (EditText) findViewById(R.id.ocjena14);
ocjena15 = (EditText) findViewById(R.id.ocjena15);
ocjena16 = (EditText) findViewById(R.id.ocjena16);
ocjena17 = (EditText) findViewById(R.id.ocjena17);
ocjena18 = (EditText) findViewById(R.id.ocjena18);
ocjena19 = (EditText) findViewById(R.id.ocjena19);
izracunaj = (Button) findViewById(R.id.izracunaj);
prosjek = (TextView) findViewById(R.id.prosjekocjena);
izracunaj.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
b=0;
if (ocjena1.getText().toString() != ""){
a1 = Integer.parseInt( ocjena1.getText().toString() );
b++;}else{a1=0;}
if (ocjena2.getText().toString() != ""){
a2 = Integer.parseInt( ocjena2.getText().toString() );
b++;}else{a2=0;}
if (ocjena3.getText().toString() != ""){
a3 = Integer.parseInt( ocjena3.getText().toString() );
b++;}else{a3=0;}
if (ocjena4.getText().toString() != ""){
a4 = Integer.parseInt( ocjena4.getText().toString() );
b++;}else{a4=0;}
if (ocjena5.getText().toString() != ""){
a5 = Integer.parseInt( ocjena5.getText().toString() );
b++;}else{a5=0;}
if (ocjena6.getText().toString() != ""){
a6 = Integer.parseInt( ocjena6.getText().toString() );
b++;}else{a6=0;}
if (ocjena7.getText().toString() != ""){
a7 = Integer.parseInt( ocjena7.getText().toString() );
b++;}else{a7=0;}
if (ocjena8.getText().toString() != ""){
a8 = Integer.parseInt( ocjena8.getText().toString() );
b++;}else{a8=0;}
if (ocjena9.getText().toString() != ""){
a9 = Integer.parseInt( ocjena9.getText().toString() );
b++;}else{a9=0;}
if (ocjena10.getText().toString() != ""){
a10 = Integer.parseInt( ocjena10.getText().toString() );
b++;}else{a10=0;}
if (ocjena11.getText().toString() != ""){
a11 = Integer.parseInt( ocjena11.getText().toString() );
b++;}else{a11=0;}
if (ocjena12.getText().toString() != ""){
a12 = Integer.parseInt( ocjena12.getText().toString() );
b++;}else{a12=0;}
if (ocjena13.getText().toString() != ""){
a13 = Integer.parseInt( ocjena13.getText().toString() );
b++;}else{a13=0;}
if (ocjena14.getText().toString() != ""){
a14 = Integer.parseInt( ocjena14.getText().toString() );
b++;}else{a14=0;}
if (ocjena15.getText().toString() != ""){
a15 = Integer.parseInt( ocjena15.getText().toString() );
b++;}else{a15=0;}
if (ocjena16.getText().toString() != ""){
a16 = Integer.parseInt( ocjena16.getText().toString() );
b++;}else{a16=0;}
if (ocjena17.getText().toString() != ""){
a17 = Integer.parseInt( ocjena17.getText().toString() );
b++;}else{a17=0;}
if (ocjena18.getText().toString() != ""){
a18 = Integer.parseInt( ocjena18.getText().toString() );
b++;}else{a18=0;}
if (ocjena19.getText().toString() != ""){
a19 = Integer.parseInt( ocjena19.getText().toString() );
b++;}else{a19=0;}
double rez = (a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18+a19)/b;
String rezString= Double.toString(rez);
prosjek.setText(rezString);
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
答案 0 :(得分:5)
请勿使用==
或!=
进行字符串比较。您正在比较对象引用而不是值。
使用equals()
,或者与""
,isEmpty()
进行比较。
答案 1 :(得分:0)
使用
if (!ocjena1.getText().toString().equals(""))
而不是
if (ocjena1.getText().toString() != "")