With .isEmpty() condition, App still crashes - java

public class MainActivity extends AppCompatActivity {
public void grade (View view){
final EditText varEditBox = (EditText)findViewById(R.id.editBox);
TextView varText = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
if (varEditBox.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(), "EMPTY", Toast.LENGTH_SHORT).show();
varText.setText( "");
}
if ((Integer.parseInt(varEditBox.getText().toString() )>=101)||(Integer.parseInt(varEditBox.getText().toString() )<1) ){
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_SHORT).show();
varText.setText( "");
} }
I have the above code with a condition to check if the EditText box is empty, but any time I press the button with an empty EditBox, the app crashes. I've been debugging but no success. Normally I get this error when I don't do the check, now I've done the check, it's still here. Plus the varEditBox isn't empty, because the other if statement runs as well. If I put in a number more than 100, it toasts "invalid" thus, the second condition works
LogCat error. I'm not sure if I have to post all of it, but this is the error I get
FATAL EXCEPTION: main
Process: com.listener.gradesystem, PID: 31936
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156) 
at android.view.View$PerformClick.run(View.java:20755) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5835) 
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:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: 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.listener.gradesystem.MainActivity.grade(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5156) 
at android.view.View$PerformClick.run(View.java:20755) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5835) 
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:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Caused by: java.lang.NumberFormatException: Invalid int: ""
One of you Integer.parseInt calls is failing. I can only assume you somehow have whitespace in your string. If your EditText only allows more than just numbers, consider doing this:
public void grade (View view){
final EditText varEditBox = (EditText)findViewById(R.id.editBox);
TextView varText = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
// trim to remove whitespace
final String text = varEditBox.getText().toString().trim();
if (TextUtils.isEmpty(text) || !TextUtils.isDigitsOnly(text)){
Toast.makeText(getApplicationContext(), "EMPTY OR NOT NUMERIC", Toast.LENGTH_SHORT).show();
varText.setText("");
// quit early, don't need to parse this number
return;
}
// we should have already quit by now if we can't parse this number
int value = Integer.parseInt(text);
if (value >= 101 || value < 1) {
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_SHORT).show();
varText.setText("");
}
}
If this still fails, at least it will highlight where your problem is.

Try this.The problem was in your if else condition.
final EditText varEditBox = (EditText)findViewById(R.id.editBox);
TextView varText = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (varEditBox.getText().toString().trim().isEmpty()){
Toast.makeText(getApplicationContext(), "EMPTY", Toast.LENGTH_SHORT).show();
varText.setText( "");
}
else{
if ((Integer.parseInt(varEditBox.getText().toString())>=101)||(Integer.parseInt(varEditBox.getText().toString() )<1) ){
Toast.makeText(getApplicationContext(), "Invalid", Toast.LENGTH_SHORT).show();
varText.setText( "");
}
}
}
});

Related

Handle exception of Blanked EditText and EditText With Character?(better get in disrciption)

I created an application for the converting Currency from Dollar to Rupee. As I entered the amount in the dollar it converts to Rupee after pressing the button. The Variable type of resulted amount is double. Now if I Entered the character in The EditText instead of Number and press the button the Application gets Crashed. The Same thing is happening when I keep the EditText blank and press the button. It is obvious that we can not convert the character into double i.e Rupee. Now How to handle these Run Time Errors by keeping about application not getting a crashed. I tried to search the similar question but not get satisfied with that. (Ask me for Extra information regarding question if you need).
ActivityMain.java
package com.example.admin.convertcurrency;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import java.util.InputMismatchException;
public class MainActivity extends AppCompatActivity {
public void ButtonClicked(View view) {
EditText EnteredAmount = (EditText) findViewById(R.id.EnteredAmount);
Log.i("Amount Entered", EnteredAmount.getText().toString());
Double ConvertedAmount = (Double.parseDouble(EnteredAmount.getText().toString()));
Double Rupees = ConvertedAmount * 71.64;
Log.i("Converted Amount", String.valueOf(Rupees));
Toast.makeText(MainActivity.this, String.format("%.2f",Rupees) + "Rs", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "Please Enter The Number Instead..!", Toast.LENGTH_SHORT).show();
}
LogCat Error Log(After typed Single "A" character in EditText)
09-09 20:30:54.415 30894-30894/com.example.admin.convertcurrency E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.convertcurrency, PID: 30894
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:5215)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:5215) 
at android.view.View$PerformClick.run(View.java:21196) 
at android.os.Handler.handleCallback(Handler.java:742) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
Caused by: java.lang.NumberFormatException: Invalid double: "a"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.admin.convertcurrency.MainActivity.ButtonClicked(MainActivity.java:44)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
at android.view.View.performClick(View.java:5215) 
at android.view.View$PerformClick.run(View.java:21196) 
at android.os.Handler.handleCallback(Handler.java:742) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
 
LogCat Error Log(After keeping the EditText as blank)
09-09 19:00:44.031 8951-8951/com.example.admin.convertcurrency E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.convertcurrency, PID: 8951
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:5215)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:5215) 
at android.view.View$PerformClick.run(View.java:21196) 
at android.os.Handler.handleCallback(Handler.java:742) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.admin.convertcurrency.MainActivity.ButtonClicked(MainActivity.java:23)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
at android.view.View.performClick(View.java:5215) 
at android.view.View$PerformClick.run(View.java:21196) 
at android.os.Handler.handleCallback(Handler.java:742) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:157) 
at android.app.ActivityThread.main(ActivityThread.java:5603) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
1- you need to force the keyboard to be numeric only and this can be done by using inputTypein you EditText XML :
android:inputType="numberDecimal"
2- you need to handle the case of empty string (blank input)
if(EnteredAmount.getText().toString().isEmpty()){
Toast.makeText(MainActivity.this, "you must enter a value to converted",
Toast.LENGTH_SHORT).show();
}
3- for making sure user does not copy a char in the EditText you can create a simple method for that :
private boolean containsChar(String input){
return input.contains("A-Za-z ");
// you can add any special chars need to avoid after A-Za-z for example A-Za-z+-*/
}
so your final onClick can be like that
String s = EnteredAmount.getText().toString();
if(s.isEmpty()){
Toast.makeText(MainActivity.this, "you must enter a value to converted",
Toast.LENGTH_SHORT).show();
}else if(containsChar(s)){
Toast.makeText(MainActivity.this, "can't contain chars",
Toast.LENGTH_SHORT).show();
}else{
// do operation here
}
To prevent anything but decimal numbers being entered into your input, add the following property to your EditText in XML:
android:inputType="numberDecimal"
To prevent your app from crashing when making a conversion, check to see if the length of the input is greater than 0:
String enteredAmount = EnteredAmount.getText().toString();
if (enteredAmount.length > 0) {
Double ConvertedAmount = (Double.parseDouble(EnteredAmount.getText().toString()));
Double Rupees = ConvertedAmount * 71.64;
Log.i("Converted Amount", String.valueOf(Rupees));
Toast.makeText(MainActivity.this, String.format("%.2f",Rupees) + "Rs", Toast.LENGTH_SHORT).show();
} else {
//do nothing or notify the user that the input can't be empty"
}

java.lang.IllegalStateException: Could not execute method for android:onClick in android

I am fairly new to Android and creating my first app. I am using using following code:
public class MainActivity extends AppCompatActivity {
int netScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addOne(View view) {
netScore = netScore + 1;
displayScore(netScore);
}
private void displayScore(int printScore) {
TextView varScore = (TextView) findViewById(R.id.score);
varScore.setText(printScore);
}
}
When I click the button, it throws this error in debug:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method)
Does anybody have an idea about this error?
Did try adding the onClick in the xml-layout like this?
android:onClick="method"
If so, make sure that there are no typos.
In your case it would probably be:
android:onClick="addOne"
You can also listen for the click events programmatically. Check out this.
You can set the TextView in OnCreate method:
TextView varScore = (TextView) findViewById(R.id.score);
after that add the ClickListener to the varScore:
enter code here
varScope.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do anything whatever you want
}
});
At first, try to initiate the TextView in the onCreate method.
BTW could you please show us your .xml file?

App crashes with empty float edittext

need help on this!
Im trying to make toast for a empty edittext, and turns out keep getting errors with the float.
Please advise
This is the code:
calcBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
**float bs = Float.parseFloat(basicSalary.getText().toString());**
float tw = Float.parseFloat(totalAllowance.getText().toString());
float mob = String spinTEXT = spinner.getSelectedItem().toString();
if (spinner.getSelectedItem().equals("Select age group")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 123",
Toast.LENGTH_SHORT).show();
}
if (basicSalary.getText().toString().equals("")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
This is the error:
java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at sg.self.wser.bonuscalc.bonuscalcu$4.onClick(bonuscalcu.java:96)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
The bonuscalc.java:96 is referring to this line of code:
float bs = Float.parseFloat(basicSalary.getText().toString());
As for the toast, i tried to change the .equals("") to .equals(" "), .equals(null) and .isEmpty, but still it wouldnt work.
Please advise!
Thanks a million!
Edited version:
Code:
if (basicSalary.getText().toString().equals(0.0)){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
Errors shown:
java.lang.NumberFormatException: Invalid float: ""
You want to test if your input is blank and if so assign the value 0.0
float bs = 0.0f;
if (basicSalary.getText().toString().equals("") == false){
bs = Float.parseFloat(basicSalary.getText().toString());
}
further down you test this for the Toast
if (basicSalary.getText().toString().equals("")){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}
maybe you could change it to
if (bs == 0.0f){
Toast.makeText(sghospitalbonuscalculator.this, "Test 321",
Toast.LENGTH_SHORT).show();
}

Cannot retrieve the data when trying to login the system (Android Studio)

I'm begginer in Android programming, I'm trying to make the login function. This system will be accessed by two users which are admin and users. The admin just need to enter the "admin" for the username and password to access the system. However, the users needs to register their account before login the system.
I'm trying to find the errors but still don't know the error occurrence. Please help me to solve the errors. Thank You.
DatabaseOperations.java
public Cursor getInformation(DatabaseOperations dob)
{
SQLiteDatabase database=dob.getReadableDatabase();
String[] columns={TableInfo.USERNAME,TableInfo.USERPASS};
Cursor cr=database.query(TableInfo.TABLE_NAME,columns,null,null,null,null,null);
return cr;
}
MainActivity.java
public void onClick(View view)
Toast.makeText(getBaseContext(), "Please wait....", Toast.LENGTH_LONG).show();
txtusername = Username.getText().toString();
txtpassword = Password.getText().toString();
DatabaseOperations dop = new DatabaseOperations(this, null, null, 1);
Cursor CR = dop.getInformation(dop);
CR.moveToFirst();
boolean login_status = false;
String NAME = "";
if (txtusername.equals("Admin") && (txtpassword).equals("Admin")) {
Intent in = new Intent(MainActivity.this, AdminMenu.class);
startActivity(in);
} else {
do {
if (txtusername.equals(CR.getString(0)) && (txtpassword.equals(CR.getString(1)))) {
login_status = true;
NAME = CR.getString(0);
}
} while (CR.moveToNext());
if (login_status) {
Toast.makeText(getBaseContext(), "Login Success....\n Welcome " + NAME, Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity.this,UserMenu.class);
Bundle bundle=new Bundle();
bundle.putString("username",NAME);
i.putExtras(bundle);
startActivity(i);
finish();
} else {
Toast.makeText(getBaseContext(), "Login Failed...", Toast.LENGTH_LONG).show();
Intent i = new Intent();
Bundle bundle=new Bundle();
bundle.putString("username",NAME);
i.putExtras(bundle);
startActivity(i);
finish();
}
}
}
This is the error...
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.google.mobile_flexi_parking, PID: 6777
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3917)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4200)
at android.app.Activity.startActivity(Activity.java:4168)
at com.google.mobile_flexi_parking.MainActivity.onclickLogin(MainActivity.java:67)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
In your else part you have blank intent which doesn't point any activity
Intent i = new Intent();
I think this is causing error.

Pass the value from activity 1 to activity 2 to do calculation and show the result

this is my first activity, i want to pass the value to second activity to do calculation and show the result in the textview.But I dont know why when i key in all the detail at activity 1, and click the custom button it jump out from the app
public void onClick(View v){
switch (v.getId())
{
case R.id.customMarco:
if(gender.getText().toString().equals("") || age.getText().toString().equals("")||
height.getText().toString().equals("")||weight.getText().toString().equals("")||
activity.getText().toString().equals("")||goal.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "Please key in all your detail above !", Toast.LENGTH_LONG).show();
}
else {
String cal = finalCal_result.getText().toString();
String carb = carb_result.getText().toString();
String protein = protein_result.getText().toString();
String fat = fat_result.getText().toString();
Intent intent = new Intent(this, CustomMarco.class);
intent.putExtra("Cal", cal);
intent.putExtra("Carb", carb);
intent.putExtra("Protein", protein);
intent.putExtra("Fat", fat);
startActivity(intent);
}
break;
}
This is my second activity, to get the valur from activity 1 and do calculation and show the result in textview cp. But I dont know why when i key in all the detail at activity 1, and click the custom button it jump out from the app
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom__marco);
cal = (EditText) findViewById(R.id.field_cal);
String calNum =getIntent().getStringExtra("Cal");
cal.setText ( calNum );
cal.setOnClickListener(this);
carb = (EditText) findViewById(R.id.field_carb);
String carbNum =getIntent().getStringExtra("Carb");
carb.setText ( carbNum );
carb.setOnClickListener(this);
Button setButton = (Button) findViewById(R.id.setButton);
setButton.setOnClickListener(this);
TextView cp = (TextView) findViewById(R.id.cp);
float cal_n = Float.parseFloat(calNum);
float carb_n = Float.parseFloat(carbNum);
int carbPValue = calculateCP(cal_n,carb_n);
cp.setText(String.valueOf(carbPValue) + " %");
}
private int calculateCP(float cal_n, float carb_n){
return (int) ((carb_n/cal_n)*4*100);
}
here the log cat error
12-03 21:25:39.284
28640-28640/com.example.sam.iifym E/AndroidRuntime:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.iifym/com.example.sam.iifym.CustomMarco}: java.lang.NumberFormatException: Invalid int: "3809 cal"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2383)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435)
at android.app.ActivityThread.access$600(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5434)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: "3809 cal"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:375)
at java.lang.Integer.parseInt(Integer.java:366)
at java.lang.Integer.parseInt(Integer.java:332)
at com.example.sam.iifym.CustomMarco.onCreate(CustomMarco.java:59)
at android.app.Activity.performCreate(Activity.java:5224)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2347)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2435) 
at android.app.ActivityThread.access$600(ActivityThread.java:168) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:194) 
at android.app.ActivityThread.main(ActivityThread.java:5434) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619) 
at dalvik.system.NativeStart.main(Native Method)
Bundle extras = getIntent().getExtras();
String carb = extras.getString("Carb");
for more info just check this answer:
https://stackoverflow.com/a/5265952/2074990
There is problem in your string.
LogCat says Invalid int: "3809 cal"
You pass Invalid data in that string.
You can just pass only Integer for convert String to Float
Example :
Valid String is "3809".
Instead of "3809 cal".
Caused by: java.lang.NumberFormatException: Invalid int: "3809 cal"
You're trying to convert the String "3809 cal" to int/float, while "3809 cal" is not a valid format of int/float.
Check your cal String in the first activity, it must be "3809" instead of "3809 cal".

Categories