我正在编写一个Android代码,我在其中输入数字(不必填写所有字段,但很少需要填写)。 单击提交按钮后,将显示结果的新活动。 我在第二个活动中也有一个按钮,用户点击后会重定向到第一个活动。

单击第一个活动中的提交按钮后,我收到以下错误

05-16 22:33:27.761: E/AndroidRuntime(1961): FATAL EXCEPTION: main
05-16 22:33:27.761: E/AndroidRuntime(1961): Process: com.example.videocalc, PID: 1961
05-16 22:33:27.761: E/AndroidRuntime(1961): java.lang.NumberFormatException: Invalid int: ""
05-16 22:33:27.761: E/AndroidRuntime(1961):     at java.lang.Integer.invalidInt(Integer.java:138)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at java.lang.Integer.parseInt(Integer.java:358)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at java.lang.Integer.parseInt(Integer.java:334)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at com.example.videocalc.MainActivity$1.onClick(MainActivity.java:33)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.view.View.performClick(View.java:4780)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.view.View$PerformClick.run(View.java:19866)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.os.Handler.handleCallback(Handler.java:739)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.os.Looper.loop(Looper.java:135)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at android.app.ActivityThread.main(ActivityThread.java:5257)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Native Method)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Method.java:372)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-16 22:33:27.761: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

MainActivity如下所示:

import android.os.Bundle;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; 
public class MainActivity extends Activity {
Button mButton;
EditText mEdit11,mEdit12,mEdit13,mEdit2,mEdit31,mEdit32,mEdit33;
TextView mText;
int hr1,min1,sec1,frm,hr2,min2,sec2,op;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButton = (Button)findViewById(R.id.button1);     
    mEdit11   = (EditText)findViewById(R.id.editText11);
    mText = (TextView)findViewById(R.id.textView1);
    mButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
    hr1=Integer.parseInt(mEdit11.getText().toString());
    min1=Integer.parseInt(mEdit12.getText().toString());
    sec1=Integer.parseInt(mEdit13.getText().toString());
    frm=Integer.parseInt(mEdit2.getText().toString());
    hr2=Integer.parseInt(mEdit31.getText().toString());
    min2=Integer.parseInt(mEdit32.getText().toString());
    sec2=Integer.parseInt(mEdit33.getText().toString());
    if (hr1==0&&min1==0&&sec1==0&&frm==0&&hr2==0&&min2==0&&sec2==0 ||   sec1==0&&frm==0&&sec2==0) {
    Toast.makeText(getActivity().getBaseContext(), "You did not enter the required fields", Toast.LENGTH_SHORT).show();
                return;
        }
            if (hr1==0&&min1==0&&hr2==0&&min2==0)
            {
                op=(sec1/(frm*sec2));
            }
            else if(hr1==0&&min1==0&&hr2==0&&min2!=0)
            {
                op=(sec1/(frm*min2*60*sec2));
            }
            else if(hr1==0&&min1!=0&&hr2==0&&min2!=0)
            {
                op=(min1*60*sec1/(frm*min2*60*sec2));
            }
            else if(hr1==0&&min1!=0&&hr2==0&&min2==0)
            {
                op=(min1*60*sec1/(frm*sec2));
            }
            else if(hr1!=0&&min1!=0&&hr2==0&&min2!=0)
            {
                op=(hr1*60*min1*60*sec1/(frm*min2*60*sec2));
            }
            Intent intent = new Intent(MainActivity.this, OpActivity.class);
            Bundle b = new Bundle();
            b.putInt("op", 1); //Your id
            intent.putExtras(b); //Put your id to your next Intent
            startActivity(intent);
            finish();
        }
      });
   }
   protected ContextWrapper getActivity() {
     // TODO Auto-generated method stub
     return null;
   }
  }

  OpActivity.java (2nd activity) goes like:

  import android.support.v7.app.ActionBarActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  public class OpActivity extends ActionBarActivity {
  @SuppressWarnings("null")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_op);
    Button mButton = null;
    Bundle b = getIntent().getExtras();
    int value = b.getInt("key");
    TextView mText = null;
    mText.setText("You need to capture the image in an interval of "+value+"!");        
    mButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
         Intent intent = new Intent(OpActivity.this, MainActivity.class);
        }
    });
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.op, menu);
   return true;
   }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  int id = item.getItemId();
  if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
     }
 }

===============>>#1 票数:2

你正在解析“”到int

05-16 22:33:27.761: E/AndroidRuntime(1961): java.lang.NumberFormatException: Invalid int: ""

===============>>#2 票数:1

您正在获取NumberFormatException因为您正在尝试将空字符串解析为int

在将其解析为int之前,您必须检查""

if(mEdit11.getText().toString()!="")
    hr1=Integer.parseInt(mEdit11.getText().toString());
else
    hr1=0; //some default value

要么

if(!mEdit11.getText().equals(""))
    hr1=Integer.parseInt(mEdit11.getText().toString());
else
    hr1=0; //some default value

类似地,你必须在所有解析之前完成这个。

UPDATE

尝试这个

if(!mEdit11.getText().toString().matches(""))
    hr1=Integer.parseInt(mEdit11.getText().toString());
else
    hr1=0; //some default value

还要记得对下面给出的代码进行检查

min1=Integer.parseInt(mEdit12.getText().toString());
sec1=Integer.parseInt(mEdit13.getText().toString());
frm=Integer.parseInt(mEdit2.getText().toString());
hr2=Integer.parseInt(mEdit31.getText().toString());
min2=Integer.parseInt(mEdit32.getText().toString());
sec2=Integer.parseInt(mEdit33.getText().toString());

===============>>#3 票数:0

hr1=Integer.parseInt(mEdit11.getText().toString());
min1=Integer.parseInt(mEdit12.getText().toString());
sec1=Integer.parseInt(mEdit13.getText().toString());
frm=Integer.parseInt(mEdit2.getText().toString());
hr2=Integer.parseInt(mEdit31.getText().toString());
min2=Integer.parseInt(mEdit32.getText().toString());
sec2=Integer.parseInt(mEdit33.getText().toString());

根据异常,您尝试解析为int的上述字符串中至少有一个为空。

您应该将这些语句放在try块中并捕获NumberFormatException。

  ask by AnRu translate from so

未解决问题?本站智能推荐: