I'm writing a android code where I input numbers (not necessary that all the fields should be filled in but few are mandatory to be filled in). After clicking on the submit button a new activity starts where result is displayed. I have a button in the 2nd activity as well, which on clicked upon by the user redirects to the 1st activity.
I get the following error after clicking on the submit button in the 1st activity
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 goes like below:
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);
}
}
你正在解析“”到int
。
05-16 22:33:27.761: E/AndroidRuntime(1961): java.lang.NumberFormatException: Invalid int: ""
You are getting NumberFormatException
because you are trying to parse an empty string into int
You have to check for ""
before parsing it to int
, like
if(mEdit11.getText().toString()!="")
hr1=Integer.parseInt(mEdit11.getText().toString());
else
hr1=0; //some default value
OR
if(!mEdit11.getText().equals(""))
hr1=Integer.parseInt(mEdit11.getText().toString());
else
hr1=0; //some default value
Similarly you'll have to do this before all the parsing..
UPDATE
Try this
if(!mEdit11.getText().toString().matches(""))
hr1=Integer.parseInt(mEdit11.getText().toString());
else
hr1=0; //some default value
also remember to do this check for the below given codes too
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());
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());
Based on the exception, at least one of the above Strings you are trying to parse to int is empty.
You should put those statements in a try block and catch NumberFormatException.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.