java.lang.numberformatexception:无效长:“”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28067237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.numberformatexception: invalid long:""
提问by Zehra
Firstly I want to connect WebServis has parameters(identification number,name,surname,date of birth).But I can't resolve this error.
首先我想连接 WebServis 有参数(身号码、姓名、姓氏、出生日期)。但我无法解决这个错误。
EditText txtTC;
EditText txtAd;
EditText txtSoyad;
EditText txtDogumYili;
Button btnDogrula;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTC=(EditText)findViewById(R.id.txtTC);
txtAd=(EditText)findViewById(R.id.txtAd);
txtSoyad=(EditText)findViewById(R.id.txtSoyad);
txtDogumYili=(EditText)findViewById(R.id.txtDogum);
btnDogrula=(Button)findViewById(R.id.btnDogrula);
btnDogrula.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Inputs that is taked from user in this class is embed in tcKnoInput
从此类中的用户获取的输入嵌入在 tcKnoInput 中
final TCKimlikNoInput tcKnoInput=new TCKimlikNoInput();
tcKnoInput.setTcKimlikNo(Long.parseLong(txtTC.getText().toString()));
tcKnoInput.setAd(txtAd.getText().toString());
tcKnoInput.setSoyad(txtSoyad.getText().toString());
tcKnoInput.setDogumYili(Integer.parseInt(txtDogumYili.getText().toString()));
TCKimlikAsyncTask lt=new TCKimlikAsyncTask();
try{
lt.input=tcKnoInput;
lt.execute("");}
catch(NumberFormatException ex){ // handle your exception
ex.printStackTrace();
}
}});
TCKimlikNoInput tcKnoInput=new TCKimlikNoInput();
tcKnoInput.setTcKimlikNo(Long.parseLong(txtTC.getText().toString()));
tcKnoInput.setAd(txtAd.getText().toString());
tcKnoInput.setSoyad(txtSoyad.getText().toString());
tcKnoInput.setDogumYili(Integer.parseInt(txtDogumYili.getText().toString()));
}
thanks for answer.But I have new error at other class.Class' code is below
谢谢你的回答。但我在其他班级有新错误。班级的代码如下
public class TCKimlikAsyncTask extends AsyncTask<String,Void,String>{
public TCKimlikNoInput input;
public boolean sonuc=false;
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
WebServiceCaller i=new WebServiceCallerImpl();
sonuc=i.TcKimlikNoDogrula(input);
return "";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), "Dogrulama Sonucu:"+sonuc, Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "Dogrulama Sonucu:"+sonuc, Toast.LENGTH_LONG).show();
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}}
then error;
然后报错;
java.lang.NullPointerException at onPostExecute is in this class
onPostExecute 处的 java.lang.NullPointerException 在这个类中
for last error,Infact I shouldn't have created getApplicationContext(),but it is not seen defined at parser
对于最后一个错误,事实上我不应该创建 getApplicationContext(),但是在解析器中没有看到它的定义
回答by Piyush
Because some of the input may be empty and you'r trying to parse it in Long. So before parse it just check your string value
因为有些输入可能是空的,而您正试图用 Long 解析它。所以在解析它之前只需检查你的字符串值
String kimlikNo = txtTC.getText().toString().trim();
if(!kimlikNo.equals(""))
{
tcKnoInput.setTcKimlikNo(Long.parseLong(kimlikNo));
}
回答by newuser
try like this,
像这样尝试,
String kimlikNo = txtTC.getText().trim();
if(!kimlikNo.isEmpty())
{
tcKnoInput.setTcKimlikNo(Long.parseLong(kimlikNo.toString()));
回答by android_developer
EditText txtTC add in xml inputType="number"
EditText txtTC 在 xml 中添加 inputType="number"
<EditText
android:id="@+id/edittext_tc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
回答by shubha
Instead of directly parsing the "txtTC.getText().toString()" to "long type, First try to check if the EditText is not empty, Then parse it to long. because you cant parse a blank string to long type.
而不是直接将"txtTC.getText().toString()"解析为"long类型,首先尝试检查EditText是否不为空,然后将其解析为long。因为您无法将空字符串解析为long类型。
replace
代替
tcKnoInput.setTcKimlikNo(Long.parseLong(txtTC.getText().toString()));
with
和
String kimlikNo = txtTC.getText().toString().trim();
if(!kimlikNo.isEmpty())
{
tcKnoInput.setTcKimlikNo(Long.parseLong(kimlikNo));
}