我尝试了太多东西但按下计算(hesapla)按钮应用程序每次崩溃。
例如:
if (vize1.getText().toString().equals("") || fina1.getText().toString().equals("") || sort1.getText().toString().equals("")){
islem_yazi.setText("FILL THE FIELDS");
} // IT DOESN'T WORK
我应该写什么/做什么?
import android.app.ActionBar;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MainActivity extends Activity {
AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
reklamiYukle();
{
}
final Button hsp_btn = (Button) findViewById(R.id.hesapla);
Button temizle = (Button) findViewById(R.id.temiz1);
final EditText vize1 = (EditText) findViewById(R.id.vize);
final EditText fina1 = (EditText) findViewById(R.id.fina);
final TextView islem_yazi = (TextView) findViewById(R.id.islem_yazi);
final EditText sort1 = (EditText) findViewById(R.id.sort);
hsp_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int v1 = Integer.parseInt(vize1.getText().toString());
int f1 = Integer.parseInt(fina1.getText().toString());
int s1 = Integer.parseInt(sort1.getText().toString());
int m;
float n, b, l, k, j, h;
float bort;
bort = (0.4f * v1) + (0.6f * f1);
m = 1;
n = 1.1f;
b = 1.18f;
l = 1.24f;
k = 1.3f;
j = 1.35f;
h = 1.4f;
if (vize1.getText().toString().equals("") || fina1.getText().toString().equals("") || sort1.getText().toString().equals("")){
islem_yazi.setText("FILL THE FIELDS");
}// IT DOES NOT WORK. APP CRASHES.
else if (s1 > 100 || s1 < 0) {
islem_yazi.setText("Sınıf Ortalaması 0 ile 100 Arası Olmalı");
}
else if (v1 > 100 || v1 < -2) {
islem_yazi.setText("Vize Notunuz -2 ile 100 Arası Olmalı");
}
else if (f1 > 100) {
islem_yazi.setText("Final/Büt Notunuz En Fazla 100 Olmalı");
}
else if (f1 < 0) {
islem_yazi.setText("Final/Büt Sınavına Girmemişsiniz");
}
else if (s1 >= 60 && m * bort <= 38) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: FF ");
} else if (s1 >= 60 && m * bort <= 45) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: FD");
} else if (s1 >= 60 && m * bort <= 52) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: DD");
} else if (s1 >= 60 && m * bort <= 59) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: DC");
} else if (s1 >= 60 && m * bort <= 66) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: CC");
} else if (s1 >= 60 && m * bort <= 73) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: CB");
} else if (s1 >= 60 && m * bort <= 80) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: BB");
} else if (s1 >= 60 && m * bort <= 87) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: BA");
} else if (s1 >= 60 && m * bort <= 140) {
islem_yazi.setText(" Bağıl Ortalamanız = " + (m * bort) + "\nHarf Notunuz: AA");
}
}
});
temizle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.temiz1);
vize1.setText("");
fina1.setText ("");
sort1.setText("");
}
});
}
private void reklamiYukle() {
adView=new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(getString(R.string.reklam));
LinearLayout layout= (LinearLayout) findViewById(R.id.reklam1);
layout.addView(adView);
AdRequest adRequest=new AdRequest.Builder().addTestDevice (AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
}
}
有一只Log Cat;
com.ertugrulguleryuzgmail.sdunothesaplama E / AndroidRuntime:FATAL 例外:主要 java.lang.NumberFormatException:无效的int:“” 在java.lang.Integer.invalidInt(Integer.java:138) 在java.lang.Integer.parseInt(Integer.java:359) 在java.lang.Integer.parseInt(Integer.java:332) 在 com.ertugrulguleryuzgmail.sdunothesaplama.MainActivity $ 1.onClick(MainActivity.java:74) 在android.view.View.performClick(View.java:4084) 在android.view.View $ PerformClick.run(View.java:16966) 在android.os.Handler.handleCallback(Handler.java:615) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:0)
vize1.getText().toString().equals("")
将其更改为"".equals(vize1.getText().toString())
(在if语句中将其应用于其余条件)
如果vize1由于任何原因(可能尚未声明)为null,编译器将抛出nullPointException并使应用程序崩溃。
答案 1 :(得分:0)
那是因为你想将空字符串转换为int。 在这里:
int v1 = Integer.parseInt(vize1.getText().toString());
int f1 = Integer.parseInt(fina1.getText().toString());
int s1 = Integer.parseInt(sort1.getText().toString());
int v1 = "".equals(vize1.getText().toString()) ? 0 : Integer.parseInt(vize1.getText().toString());
答案 2 :(得分:0)
使用isEmpty()
方法测试条件。它使用的代码更少,更清楚你的意图。