当我想获取整数值并将其传递给电子邮件时,java应用程序崩溃
我试图从editText向电子邮件传递整数值,但应用程序一直崩溃
public class EditProductActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {`
private EditText mQuantityEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
// Initializing all the view variables associated to both product activity "modes".
mNameEditText = (EditText) findViewById(R.id.ProductNameEditText);
mSupplierEditText = (EditText) findViewById(R.id.SupplierEditView);
mPriceEditText = (EditText) findViewById(R.id.PriceEditText);
mQuantityEditText = (EditText) findViewById(R.id.QuantityEditText);
mProductImageView = (ImageView) findViewById(R.id.ProductImageView);
// Set onTouchListeners for all editviews/imageviews
mNameEditText.setOnTouchListener(mTouchListener);
mQuantityEditText.setOnTouchListener(mTouchListener);
mSupplierEditText.setOnTouchListener(mTouchListener);
mPriceEditText.setOnTouchListener(mTouchListener);
mProductImageView.setOnTouchListener(mTouchListener);
// If data is passed by intent, then display the current product in questions information\
// Also known as "Edit mode"
if (getIntent().getData() != null) {
int i =Integer.parseInt(mQuantityEditText.getText().toString());
Log.i("i===", String.valueOf(i));
// Defining all the variables required for just the editing process
mQuantityTextView = (TextView) findViewById(R.id.displayQuantity);
mSaleButton = (Button) findViewById(R.id.SaleButton);
mIncrementButton = (ImageButton) findViewById(R.id.IncrementButton);
mDecrementButton = (ImageButton) findViewById(R.id.DecrementButton);
mOrderButton = (ImageButton) findViewById(R.id.OrderButton);
// Adding additional onTouchListeners to the newly tracked variables:
mSaleButton.setOnTouchListener(mTouchListener);
mIncrementButton.setOnTouchListener(mTouchListener);
mDecrementButton.setOnTouchListener(mTouchListener);
// Since we are in editmode, can hide the quantity editText and replace it with the
// quantity controller view layout.
mQuantityEditText.setVisibility(View.GONE);
findViewById(R.id.QantityLayout).setVisibility(View.VISIBLE);
mCurrentProductUri = getIntent().getData();
setTitle(getString(R.string.edit_product));
// Handle the various quantity alteration options
mSaleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alterQuantity(-1);
}
});
mIncrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alterQuantity(1);
}
});
mDecrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alterQuantity(-1);
}
});
//下面,我试图从编辑文本中获取整数值,但当我按“订单”按钮将其发送到电子邮件时,应用程序崩溃
// Handle the order button request
mOrderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里我再试一次:
public void onClick(View v) {
int i =Integer.parseInt(mQuantityEditText.getText().toString());
Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
emailIntent.setData(Uri.parse("mailto:" + mSupplierEditText.getText().toString()));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Product Order");
emailIntent.putExtra(Intent.EXTRA_TEXT, "I would like to order "+String.valueOf(i));
try {
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
} catch (安卓.content.ActivityNotFoundException ex) {
Toast.makeText(EditProductActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
LOGCAT错误
06-16 05:02:29.790 4135-4135/com.example.mohammedragab.productinventoryapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.mohammedragab.productinventoryapp, PID: 4135 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mohammedragab.productinventoryapp/com.example.mohammedragab.productinventoryapp.EditProductActivity}: java.lang.NumberFormatException: For input string: "" at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at 安卓.app.ActivityThread.-wrap12(ActivityThread.java) at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at 安卓.os.Handler.dispatchMessage(Handler.java:102) at 安卓.os.Looper.loop(Looper.java:154) at 安卓.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NumberFormatException: For input string: "" at java.lang.Integer.parseInt(Integer.java:533) at java.lang.Integer.parseInt(Integer.java:556) at com.example.mohammedragab.productinventoryapp.EditProductActivity.onCreate(EditProductActivity.java:104) at 安卓.app.Activity.performCreate(Activity.java:6679) at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at 安卓.app.ActivityThread.-wrap12(ActivityThread.java) at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at 安卓.os.Handler.dispatchMessage(Handler.java:102) at 安卓.os.Looper.loop(Looper.java:154) at 安卓.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:776)
# 1 楼答案
您的应用程序正在崩溃,因为inputField是空字符串,如错误日志所示:
java.lang.NumberFormatException: For input string: ""
这是因为
onCreate()
——它实例化了TextInput
字段,这意味着当您尝试getText()
时,它将是空的。这就解释了NumberFormatException
被提出的原因int i =Integer.parseInt(mQuantityEditText.getText().toString());
要修复它:
从
onCreate()
中删除这一行,因为同一行已经存在于onClick()
事件处理程序中int i =Integer.parseInt(mQuantityEditText.getText().toString());
以下是单击处理程序的工作代码: