我有一个应用程序在单击计算按钮时崩溃。在程序中,用户从下拉菜单列表中输入食物信息,然后将其信息与订单一起输入。然后使用文本计算订单总数,并在第二个活动(Food_Total)中显示带有订单信息的Toast消息。
>>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.
答案 0 :(得分:0)
AndroidManifest.xml中如何定义这些活动? MainActivity应该有一个intent过滤器,如下所示: 另一个Activity也必须在那里,但不应该有一个意图过滤器。