gpt4 book ai didi

android - 单击计算按钮时程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:39:53 24 4
gpt4 key购买 nike

I have an application that crashes when the calculate button is clicked. In the program, a user enters the food information from a drop down menu list and then enters their information along with the order. The order total is then calculated with text and the toast message displays with their order information in the second activity (Food_Total).


        >>Here are print screens of the code:

public class MainActivity extends Activity {

public static String priceString= "";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public void onClick(View view) {
Intent intent = new Intent(this, Food_Total.class);
Bundle b = new Bundle();
DecimalFormat df = new DecimalFormat("0.00");//decimal format for dollars
EditText mealNameBox= (EditText) findViewById(R.id.editText1);
String mealNameString= mealNameBox.getText().toString();
EditText mealPriceBox = (EditText) findViewById(R.id.editText2);
String priceString = mealPriceBox.getText().toString();
Double priceDouble= Double.parseDouble(priceString);

//Sales tax for VA
String tax= "4%";

String percentString= tax.toString();
//Calculate taxes for food
double taxfood= priceDouble * .04;

String taxfood_string= Double.toString(taxfood);


//Calculate food total
double food_total= taxfood + priceDouble;

String food_total_string= Double.toString(food_total);

b.putString("Meal Price:", "" + df.format(priceString));
b.putString("Tax: ", tax);
b.putString("Food Total: ", "" + df.format(food_total));

intent.putExtras(b);
startActivity(intent);

//Toast.makeText(this, "" + tip, Toast.LENGTH_LONG).show();
//Toast.makeText(this, "" + total, Toast.LENGTH_LONG).show();
}
}


Food_Total Activity (displays toast message):

public class Food_Total extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food__total);
//get info from intent
Bundle b = getIntent().getExtras();

// #####################################################
//Updated key values on bundle so that they
// match the MainActivity class
// #####################################################
String meal_nameString= b.getString("MealName");
String mealString = b.getString("MealPrice");
String percentString = b.getString("TaxRate");
String tax_foodString= b.getString("TaxAmount");
String food_totalString= b.getString("Total");

//results output info
String results = meal_nameString + getString(R.string.mealNameString) +
"\n" + mealString + getString(R.string.mealPrice)+ ": $"+
"\n" + percentString + "%" +
"\n" + tax_foodString + getString(R.string.taxfood_string) + ": $" +
"\n" + food_totalString + getString(R.string.food_total_string);

//create text view
TextView textView = new TextView(this);
textView.setTextSize(30);
textView.setText(results);

//set view as the activity layout
setContentView(textView);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_food__total, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}

Layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/name_lbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/name"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="Student Name: "
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/Food_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name_lbl"
android:layout_below="@+id/name_lbl"
android:layout_marginTop="34dp"
android:text="Food Name:"
android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Food_Name"
android:layout_alignParentRight="true"
android:ems="10" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Food_Name"
android:layout_below="@+id/Food_Name"
android:layout_marginTop="26dp"
android:text="Food Price: "
android:textAppearance="?android:attr/textAppearanceSmall" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="numberDecimal" />

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/name_lbl"
android:layout_below="@+id/editText2"
android:layout_marginTop="44dp" >

</ListView>

<Button
android:id="@+id/calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/listView1"
android:layout_marginRight="50dp"
android:onClick="onClick"
android:text="Calculate" />

</RelativeLayout>

>>Android Manifest File:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.food_activity_final"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Food_Total"
android:label="@string/title_activity_food__total" >

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</activity>
</application>

</manifest>

>>Thank you for your help! Let me know if you need to know anything more about the program.

最佳答案

这些 Activity 如何在AndroidManifest.xml中定义? MainActivity应该具有一个意图过滤器,如下所示:




其他 Activity 也必须在其中,但它不应具有意图过滤器。

关于android - 单击计算按钮时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13128132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com