片段中的数字格式异常错误
numberformatexception error in fragment
我是 android 方面的新手,真的需要帮助
我尝试按照其他人在类似 post 中所说的进行操作,但我无法使其工作,如果 EditText
单击按钮后我 运行 它仍然弹出错误(模态)字段为空。
我在这段代码中使用片段,因为另一个 post 使用 activity,我不知道我错过了什么或者我错在哪里,请帮助我...
这是我的代码
public class Menu_PIAF extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView)inflater.inflate(R.layout.piaf_layout,
container, false);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//declare
final EditText modal = (EditText) getView().findViewById(R.id.value_modal);
TextView result_piaf = (TextView) getView().findViewById(R.id.result_piaf);
TextView abc = (TextView) getView().findViewById(R.id.notice);
final String modalx = String.valueOf(modal.getText());
//convert integer
int modaly = new Integer(Integer.parseInt(modalx));
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
else{
notice.setText("Error");
} }
});
return mScrollView;
}}
Logcat :
Process: com.example.fabio.tabdrawer, PID: 20089
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.fabio.tabdrawer.Menu_PIAF.onClick(Menu_PIAF.java:65)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
试试这个:
public class Menu_PIAF extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView) inflater.inflate(R.layout.piaf_layout, container, false);
EditText modal = (EditText) mScrollView.findViewById(R.id.value_modal);
TextView result_piaf = (TextView) mScrollView.findViewById(R.id.result_piaf);
TextView abc = (TextView) mScrollView.findViewById(R.id.notice);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//declare
String modalx = modal.getText().toString();
if(!modalx.equals("")) {
int modaly = Integer.parseInt(modalx);
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
} else {
notice.setText("Error");
}
}
}
});
return mScrollView;
}
我是 android 方面的新手,真的需要帮助
我尝试按照其他人在类似 post 中所说的进行操作,但我无法使其工作,如果 EditText
单击按钮后我 运行 它仍然弹出错误(模态)字段为空。
我在这段代码中使用片段,因为另一个 post 使用 activity,我不知道我错过了什么或者我错在哪里,请帮助我...
这是我的代码
public class Menu_PIAF extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView)inflater.inflate(R.layout.piaf_layout,
container, false);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//declare
final EditText modal = (EditText) getView().findViewById(R.id.value_modal);
TextView result_piaf = (TextView) getView().findViewById(R.id.result_piaf);
TextView abc = (TextView) getView().findViewById(R.id.notice);
final String modalx = String.valueOf(modal.getText());
//convert integer
int modaly = new Integer(Integer.parseInt(modalx));
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
else{
notice.setText("Error");
} }
});
return mScrollView;
}}
Logcat :
Process: com.example.fabio.tabdrawer, PID: 20089
java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:334)
at com.example.fabio.tabdrawer.Menu_PIAF.onClick(Menu_PIAF.java:65)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
试试这个:
public class Menu_PIAF extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ScrollView mScrollView = (ScrollView) inflater.inflate(R.layout.piaf_layout, container, false);
EditText modal = (EditText) mScrollView.findViewById(R.id.value_modal);
TextView result_piaf = (TextView) mScrollView.findViewById(R.id.result_piaf);
TextView abc = (TextView) mScrollView.findViewById(R.id.notice);
Button button_submit = (Button) mScrollView.findViewById(R.id.button_submit);
button_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//declare
String modalx = modal.getText().toString();
if(!modalx.equals("")) {
int modaly = Integer.parseInt(modalx);
if (modaly >= 5000) {
String piaf = "Deposit";
result_piaf.setText(piaf);
notice.setText("Succeed");
} else {
notice.setText("Error");
}
}
}
});
return mScrollView;
}