[英]Android NullPointerException When retriving Spinner text
I'm trying to get the selected item of a spinner and convert it into a string then save it as a .txt
file then send, everything works fine until I implemented the spinner and now I'm getting a null pointer exception when I selected No and nothing happens when I select Yes. 我正在尝试获取微调器的选定项并将其转换为字符串,然后将其保存为.txt
文件,然后发送,一切正常,直到实现了微调器,现在选择时出现了空指针异常否,当我选择“是”时,什么也不会发生。 The Lines referenced in the Logcat are the methods being excecuted in Updater()
, and the clickedUpdate
is the the Updater()
method being run.I'm 100% sure the error occurs in the Updater() Method. 在logcat中引用的线是在被excecuted方法Updater()
并且clickedUpdate
是所述Updater()
方法是run.I'm 100%确定在更新程序()方法中发生错误。
The Updater() Method Updater()方法
public String Updater()
{
Vibrator vibrate = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
TextView dLong = (TextView) findViewById(R.id.textLong);
TextView dLat = (TextView) findViewById(R.id.textLat);
EditText xxxxxx = (EditText)findViewById(R.id.POinput);
EditText xxxxxx = (EditText)findViewById(R.id.splitPOinput);
Spinner xxxxxchoice = (Spinner) findViewById(R.id.optionselecti);
String xxxxxx = xxxxxx.getText().toString();
String dataLat = dLat.getText().toString();
String dataLong = dLong.getText().toString();
String xxxxxx = xxxxxx.getText().toString();
String Update = null;
TelephonyManager telephonemanager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String PhoneNumber = telephonemanager.getLine1Number();
String xxxxxxChoicei = xxxxxxchoice.getSelectedItem().toString();
int choice = 0;
boolean Choice;
if(xxxxxxChoicei.equals("Yes"))
{
choice = 1;//virtual number used to indicate spinner choice on data server
Choice = true; //for if statment below
vibrate.vibrate(100);
}
else if (xxxxxxChoicei.equals("No"))
{
choice = 0;
Choice = false;
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Temperature");
alert.setMessage("Input Temperature");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
alert.setView(input);
alert.setPositiveButton("Check-In", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
temperature = input.getText().toString();
}
});
if(Choice = false)
{
alert.show();
}
if(xxxxxxxxxChoicei.equals("No"))
{
temperature = "DRY";
}
String DataIn = PhoneNumber + "," + dataLong + "," + dataLat+","+dataPO+","+splitpo+","+temperature+","+choice;
try {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
File(getFilesDir() + File.separator + "xxxxxxx_LOCATION_DATA.txt")));
bufferedWriter.write(DataIn);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
DATAOUT();
while((!(temperature.equals(""))&& temperature!=null) || xxxxxxxChoicei.equals("No"))
{
sendMail("xxxxxxxxxxxxxxx@gmail.com", "DataTest", DATAOUT());
Update = "Updated";
deleteFile("xxxxxxx_LOCATION_DATA.txt");
}
logcat 日志猫
Caused by: java.lang.NullPointerException
at javax.mail.internet.MimeUtility.checkAscii(MimeUtility.java:1346)
at javax.mail.internet.MimeBodyPart.setText(MimeBodyPart.java:1069)
at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1493)
at javax.mail.internet.MimeMessage.setText(MimeMessage.java:1477)
at com.example.adrian.trucktracker.Locator.createMessage(Locator.java:134)
at com.example.adrian.trucktracker.Locator.sendMail(Locator.java:170)
at com.example.adrian.trucktracker.Locator.Updater(Locator.java:256)
at com.example.adrian.trucktracker.Locator.clickedUpdate(Locator.java:264)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3860)
at android.view.View.performClick(View.java:4480)
at android.view.View$PerformClick.run(View.java:18686)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
So after fixing it so that DATAOUT() no longer returned null (by setting the default string value to String Dataout = "";
) I got a bunch of errors caused by this statement: 因此,在修复它使DATAOUT()不再返回null之后(通过将默认字符串值设置为String Dataout = "";
),我得到了由该语句引起的一堆错误:
while((!(temperature.equals(""))&& temperature!=null) || xxxxxxxChoicei.equals("No")) { sendMail("xxxxxxxxxxxxxxx@gmail.com", "DataTest", DATAOUT()); Update = "Updated"; deleteFile("xxxxxxx_LOCATION_DATA.txt"); }
which caused an infinite loop, so i fixed that by no longer using the while, however does someone know how to include the while loop without causing my application to crash? 这导致了无限循环,所以我通过不再使用while来解决了这一问题,但是有人知道如何包括while循环而不会导致我的应用程序崩溃吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.