-8
down vote
favorite
Im a noob in this so please bear with me. I have a listview of food and drinks with price then a proceed button which will show the order summary and total amount in other activity. After I click some items then click the proceed button, the app crash "Unfortunately appname has stopped." But when I click proceed button without clicking any items it displays the layout of the order summary. Therefore the error occurs in the passing of data. In logcat I noticed first Invalid float: "" I have no idea how to fix this.
What I have tried:
package edu.sti.snapchit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.StringTokenizer;
import java.lang.String;
public class TotalsActivity extends AppCompatActivity{
MyApp mApp;
EditText et_summary;
TextView tv_total;
Button btn_code;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.totals);
btn_code = (Button) findViewById(R.id.btn_code);
mApp=((MyApp)getApplicationContext());
et_summary = (EditText) findViewById(R.id.et_summary);
tv_total = (TextView) findViewById(R.id.tv_total);
et_summary.setText(mApp.getGlobalVarValue());
String str = mApp.getGlobalVarValue();
StringTokenizer st = new StringTokenizer(str,"Php");
String test="";
float total=0;
int count =0;
while(st.hasMoreElements())
{
test = st.nextElement().toString().substring(0,1);
if(count>0)
total += Float.parseFloat(test);
count++;
}
tv_total.setText("Total:" + total+"");
mApp.setGlobalClear();
btn_code.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(TotalsActivity.this, GeneratorActivity.class);
startActivity(intent);
}
});
}
public boolean isFloat(String input)
{
try
{
Float.parseFloat(input);
return true;
}
catch(Exception e)
{
return false;
}
}
}
Code for Food Activity which contains the listview
package edu.sti.snapchit;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.HashMap;
public class NewFoodsActivity extends AppCompatActivity {
MyApp mApp;
private HashMap<String, Location> locations;
ListView listView1;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=openOrCreateDatabase("Foods_DB", Context.MODE_PRIVATE, null);
setContentView(R.layout.new_foods);
locations = loadLocationData();
addListenerButton();
initializeUI();
}
public void initializeUI()
{
String[] foodies = getFoodNames();
ArrayAdapter<string> adapter = new ArrayAdapter<string>
(this,android.R.layout.simple_list_item_1, foodies);
listView1.setAdapter(adapter);
}
private String[] getFoodNames()
{
String[] foodies = new String[locations.size()];
foodies = locations.keySet().toArray(foodies);
return foodies;
}
private void displaySelectedFoodInfo(String foodName)
{
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
private HashMap<String, Location>loadLocationData()
{
HashMap<String, Location>locations = new HashMap<String, Location>();
Cursor c=db.rawQuery("SELECT * FROM table_foods order by food_id
asc",null);
StringBuffer buffer = new StringBuffer();
while(c.moveToNext())
{
locations.put("- "+c.getString(1).toString()+" [Php
"+c.getString(2).toString()+"]", new Location(Integer.parseInt(c.getString(0)),c.getString(1).toString(),Double.parseDouble(c.getString(2))));
}
return locations;
}
public void addListenerButton()
{
final Context context = this;
Button btnAdd = (Button)findViewById(R.id.btn_add);
Button btnProceed = (Button)findViewById(R.id.btnProceed);
Button btn_drinks = (Button)findViewById(R.id.btn_drinks);
Button btn_foods = (Button)findViewById(R.id.btn_foods);
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> Arg0, View view, int
position, long id) {
Object o = listView1.getItemAtPosition(position);
String pen = o.toString();
mApp=((MyApp)getApplicationContext());
mApp.setGlobalVarValue(pen);
Toast.makeText(getApplicationContext(), "Item Added" +
""+ pen, Toast.LENGTH_LONG).show();
}
}
);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NewAddFoods.class);
startActivity(intent);
}
});
btnProceed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, TotalsActivity.class);
startActivity(intent);
}
});
btn_foods.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NewFoodsActivity.class);
startActivity(intent);
}
});
btn_drinks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, NewDrinksActivity.class);
startActivity(intent);
}
});
}
}
This is the colored red text in LOGCAT
02-20 04:37:23.624 1447-1447/edu.sti.snapchit E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.sti.snapchit/edu.sti.snapchit.TotalsActivity}: java.lang.NumberFormatException: Invalid float: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) 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:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid float: "" at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseFloat(StringToReal.java:289) at java.lang.Float.parseFloat(Float.java:300) at edu.sti.snapchit.TotalsActivity.onCreate(TotalsActivity.java:40) at android.app.Activity.performCreate(Activity.java:5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-20 04:37:29.372 1494-1494/edu.sti.snapchit E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-20 04:37:30.592 1494-1494/edu.sti.snapchit E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache 02-20 04:37:30.616 1494-1494/edu.sti.snapchit E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
Ps. Yes I did followed a youtube tutorial in making this app. The app in vid is working perfectly while mine crashes. Either I missed something but I checked the code like 10 times or either there are parts of the vid been skipped. The uploader havent respond for days :(
I almost forgot there might also another reason that caused this. In TotalsActivity the method isFloat is never used.