首页 文章

当我想获取整数值并将其传递给电子邮件意图时,应用崩溃

提问于
浏览
0

我试图将整数值传递给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 (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(EditProductActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }
        });

LOGCAT ERROR

06-16 05:02:29.790 4135-4135 / com.example.mohammedragab.productinventoryapp E / AndroidRuntime:FATAL EXCEPTION:main进程:com.example.mohammedragab.productinventoryapp,PID:4135 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mohammedragab.productinventoryapp / com.example.mohammedragab.productinventoryapp.EditProductActivity}:java.lang.NumberFormatException:对于输入字符串:“”在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)at Android.app上的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)位于android.app.A活动上的android.app.ActivityThread.-wrap12(ActivityThread.java)$ H.handleMessage(ActivityThread.java:1477) . 位于android.app.Looper.loop(Looper.java:154)的Handler.dispatchMessage(Handler.java:102)位于android.app.ActivityThread.main(ActivityThread.java:6119)的java.lang.reflect.Method.invoke (本地方法)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)引起:java.lang.NumberFormatException:对于输入字符串:java“java.lang.Integer.parseInt(Integer.java:533) .lang.Integer.parseInt(Integer.java:556)位于android的android.app.Activity.performCreate(Activity.java:6679)的com.example.mohammedragab.productinventoryapp.EditProductActivity.onCreate(EditProductActivity.java:104) . 在Android.app.ActivityThread.-的android.app.ActivityThread.perleLaunchActivity(ActivityThread.java:2726)的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)上的app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)在android.O.上运行android.app.A.运行时,Android.O.Roper.loop(Looper.java)上的android.app.A.运行会员处于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)的wrap12(ActivityThread.java)(Handler.java:102) :154)位于com.android.internal.os.ZygoteInit的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:6119) $ MethodAndArgsCaller.run(ZygoteInit.java:886)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

1 回答

  • 1

    您的应用程序崩溃,因为inputField是一个空字符串,如错误日志中所示:

    java.lang.NumberFormatException: For input string: ""

    这是因为你的 onCreate() - 它正在实例化 TextInput 字段,这意味着当你尝试 getText() 时它将是空的 . 这就解释了为什么 NumberFormatException 被提出来了 .

    int i =Integer.parseInt(mQuantityEditText.getText().toString());

    To fix it:

    onCreate() 中删除此行,因为 onClick() 事件处理程序中已存在该行 .

    int i =Integer.parseInt(mQuantityEditText.getText().toString());

    以下是您的点击处理程序的工作代码:

    mOrderButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String textFromInput = mQuantityEditText.getText().toString();
            String supplierText  = mSupplierEditText.getText().toString();
            if(!textFromInput.isEmpty() && !supplierText.isEmpty()){
                int i =Integer.parseInt(textFromInput);
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
                emailIntent.setData(Uri.parse("mailto:" + supplierText));
                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 (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(EditProductActivity.this, "No email clients installed.", Toast.LENGTH_SHORT).show();
                }
            }else{
                //The textfield is empty, do something about it...
            }
         }
    });
    

相关问题