如何在android中进行乘法?

时间:2014-11-08 08:34:00

标签: android eclipse

我目前正在开发一个Android应用程序。我一开始没有任何问题,但是当我运行我的代码时,页面意外停止。首先,我运行mcpakej1.xml。然后在我点击提交按钮而不在编辑文本中输入任何值后,页面意外停止。当我看到logcat中的错误时,错误发生在其他页面。下面是代码。

mcpakej1.xml

 <?xml version="1.0" encoding="utf-8"?>

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

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:text="Package A \n"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
      android:id="@+id/textView50"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Number of guest(s)"/>

<EditText 
    android:id="@+id/guest"
    android:layout_width="fill_parent"
    android:layout_height="50dp"   
    android:inputType="number"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:hint="Maximum : 10000"   >

    <requestFocus />
</EditText>

 </LinearLayout> 

 </ScrollView>

 <ImageButton
        android:id="@+id/homebtn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/homebtn"
        android:layout_toLeftOf="@+id/homebtn"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
        android:background="@drawable/homebtn"
        android:onClick="goHome" />

<ImageButton
        android:id="@+id/gobutton"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/homebtn"
        android:layout_toRightOf="@+id/homebtn"
    android:layout_alignParentBottom="true"
        android:background="@drawable/gobutton"
        android:onClick="goReceipt"/>

<ImageButton
        android:id="@+id/backbutton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/homebtn"
    android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/homebtn"
        android:background="@drawable/backbutton"
        android:onClick="goPackage" 
        android:gravity="right"/>




 </RelativeLayout>

这是mcpakej1.java

  public class mcpakej1 extends Activity 
  {
private EditText passEditText;


CheckBox nsPutih,nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,
prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,
kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih;

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

  passEditText = (EditText) findViewById(R.id.guest);


  nsPutih       = (CheckBox) findViewById(R.id.pakejA1);
  nsBeriyani    = (CheckBox) findViewById(R.id.pakejA2);
  nsMinyak      = (CheckBox) findViewById(R.id.pakejA3);
  aymKurma      = (CheckBox) findViewById(R.id.pakejA4);
  aymLemak      = (CheckBox) findViewById(R.id.pakejA5);
  aymKari       = (CheckBox) findViewById(R.id.pakejA6);
  aymRendang    = (CheckBox) findViewById(R.id.pakejA7);
  aymMerah      = (CheckBox) findViewById(R.id.pakejA8);
  prtAsam       = (CheckBox) findViewById(R.id.pakejA9);
  dggTomato     = (CheckBox) findViewById(R.id.pakejA10);
  dggSinggang   = (CheckBox) findViewById(R.id.pakejA11);
  dggMerah      = (CheckBox) findViewById(R.id.pakejA12);
  kerabuTaugeh  = (CheckBox) findViewById(R.id.pakejA13);
  pindangKacang = (CheckBox) findViewById(R.id.pakejA14);
  jelatah       = (CheckBox) findViewById(R.id.pakejA15);
  dalcaSayur    = (CheckBox) findViewById(R.id.pakejA16);
  papadom       = (CheckBox) findViewById(R.id.pakejA17);
  kariIkan      = (CheckBox) findViewById(R.id.pakejA18);
  ikanMasin     = (CheckBox) findViewById(R.id.pakejA19);
  sirapAis      = (CheckBox) findViewById(R.id.pakejA20);
  oren          = (CheckBox) findViewById(R.id.pakejA21);
  tehTarik      = (CheckBox) findViewById(R.id.pakejA22);
  agarAgar      = (CheckBox) findViewById(R.id.pakejA23);
  buburKacang   = (CheckBox) findViewById(R.id.pakejA24);
  kuih          = (CheckBox) findViewById(R.id.pakejA25);


}

public void goHome(View v){
{
    Intent intent = new Intent(v.getContext(), MainActivity.class);
    startActivityForResult(intent,0);}
}

public void goReceipt(View v) 
{

    Intent intent = new Intent(v.getContext(), doReceipt.class);

    intent.putExtra("nsBeriyani", nsBeriyani.isChecked());
    intent.putExtra("nsPutih", nsPutih.isChecked());
    intent.putExtra("nsMinyak", nsMinyak.isChecked());
    intent.putExtra("aymKurma", aymKurma.isChecked());
    intent.putExtra("aymKari", aymKari.isChecked());
    intent.putExtra("aymRendang", aymRendang.isChecked());
    intent.putExtra("aymMerah", aymMerah.isChecked());
    intent.putExtra("prtAsam", prtAsam.isChecked());
    intent.putExtra("dggTomato", dggTomato.isChecked());
    intent.putExtra("dggSinggang", dggSinggang.isChecked());
    intent.putExtra("dggMerah", dggMerah.isChecked());
    intent.putExtra("kerabuTaugeh", kerabuTaugeh.isChecked());
    intent.putExtra("pindangKacang", pindangKacang.isChecked());
    intent.putExtra("jelatah", jelatah.isChecked());
    intent.putExtra("dalcaSayur", dalcaSayur.isChecked());
    intent.putExtra("papadom", papadom.isChecked());
    intent.putExtra("kariIkan", kariIkan.isChecked());
    intent.putExtra("ikanMasin", ikanMasin.isChecked());
    intent.putExtra("sirapAis", sirapAis.isChecked());
    intent.putExtra("oren", oren.isChecked());
    intent.putExtra("tehTarik", tehTarik.isChecked());
    intent.putExtra("agarAgar", agarAgar.isChecked());
    intent.putExtra("buburKacang", buburKacang.isChecked());
    intent.putExtra("kuih", kuih.isChecked());


    findViewById(R.id.gobutton).setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {   final String pass = passEditText.getText().toString();
            if (!isValidPassword(pass)) 
            {
                passEditText.setError("Invalid Number");
            }
        }
    });
    startActivityForResult(intent,0);
}
public void goPackage(View v){
    {
        Intent intent = new Intent(v.getContext(), PackageSelect.class);
        startActivityForResult(intent,0);}
    }

// validating password with retype password
private boolean isValidPassword(String pass) 
{
    if (pass != null && pass.length() <= 10000) {
        return true;
    }
    return false;
 }}

这是我点击提交按钮后在logcat中收到的错误。

11-22 10:38:04.434: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)
11-22 10:38:04.434: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException
11-22 10:38:04.434: E/AndroidRuntime(1173):     at com.example.mobilecatering.doReceipt.onCreate(doReceipt.java:398)
11-22 10:38:04.434: E/AndroidRuntime(1173):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 10:38:04.434: E/AndroidRuntime(1173):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-22 10:38:04.434: E/AndroidRuntime(1173):     ... 11 more
11-22 10:38:06.294: I/Process(1173): Sending signal. PID: 1173 SIG: 9

这是错误状态为doReceipt.java的页面

  public class doReceipt extends Activity

{
boolean nsPutih, nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,
prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,
kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih, price;



TextView tvOutput1,tvOutput2, tvOutput3, tvOutput4, tvOutput5, tvOutput6, tvOutput7, tvOutput8, tvOutput9, tvOutput10, 
tvOutput11, tvOutput12, tvOutput13, tvOutput14, tvOutput15, tvOutput16, tvOutput17, tvOutput18, tvOutput19, tvOutput20,
tvOutput21, tvOutput22, tvOutput23, tvOutput24, tvOutput25, tvOutput26, tvOutput27, tvOutput28;

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

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            return;
        }

        // assign the values to string-arguments
        String phone = extras.getString("phone");

        TextView phoneText = (TextView) findViewById(R.id.textView27);      
        phoneText.setText(phone);   

      tvOutput1 = (TextView) findViewById(R.id.textView1);
      tvOutput2 = (TextView) findViewById(R.id.textView2);
      tvOutput3 = (TextView) findViewById(R.id.textView3);
      tvOutput4 = (TextView) findViewById(R.id.textView4);
      tvOutput5 = (TextView) findViewById(R.id.textView5);
      tvOutput6 = (TextView) findViewById(R.id.textView6);
      tvOutput7 = (TextView) findViewById(R.id.textView7);
      tvOutput8 = (TextView) findViewById(R.id.textView8);
      tvOutput9 = (TextView) findViewById(R.id.textView9);
      tvOutput10 = (TextView) findViewById(R.id.textView10);
      tvOutput11 = (TextView) findViewById(R.id.textView11);
      tvOutput12 = (TextView) findViewById(R.id.textView12);
      tvOutput13 = (TextView) findViewById(R.id.textView13);
      tvOutput14 = (TextView) findViewById(R.id.textView14);
      tvOutput15 = (TextView) findViewById(R.id.textView15);
      tvOutput16 = (TextView) findViewById(R.id.textView16);
      tvOutput17 = (TextView) findViewById(R.id.textView17);
      tvOutput18 = (TextView) findViewById(R.id.textView18);
      tvOutput19 = (TextView) findViewById(R.id.textView19);
      tvOutput20 = (TextView) findViewById(R.id.textView20);
      tvOutput21 = (TextView) findViewById(R.id.textView21);
      tvOutput22 = (TextView) findViewById(R.id.textView22);
      tvOutput23 = (TextView) findViewById(R.id.textView23);
      tvOutput24 = (TextView) findViewById(R.id.textView24);
      tvOutput25 = (TextView) findViewById(R.id.textView25);
      tvOutput26 = (TextView) findViewById(R.id.textView26);
      tvOutput27 = (TextView) findViewById(R.id.textView27);
      tvOutput28 = (TextView) findViewById(R.id.textView28);


      Bundle data = this.getIntent().getExtras();

      nsPutih=data.getBoolean("nsPutih");
      nsBeriyani=data.getBoolean("nsBeriyani");
      nsMinyak=data.getBoolean("nsMinyak");
      aymKurma=data.getBoolean("aymKurma");
      aymLemak=data.getBoolean("aymLemak");
      aymKari=data.getBoolean("aymKari");
      aymRendang=data.getBoolean("aymRendang");
      aymMerah=data.getBoolean("aymMerah");
      prtAsam=data.getBoolean("prtAsam");
      dggTomato=data.getBoolean("dggTomato");
      dggSinggang=data.getBoolean("dggSinggang");
      dggMerah=data.getBoolean("dggMerah");
      kerabuTaugeh=data.getBoolean("kerabuTaugeh");
      pindangKacang=data.getBoolean("pindangKacang");
      jelatah=data.getBoolean("jelatah");
      dalcaSayur=data.getBoolean("dalcaSayur");
      papadom=data.getBoolean("papadom");
      kariIkan=data.getBoolean("kariIkan");
      ikanMasin=data.getBoolean("ikanMasin");
      sirapAis=data.getBoolean("sirapAis");
      oren=data.getBoolean("oren");
      tehTarik=data.getBoolean("tehTarik");
      agarAgar=data.getBoolean("agarAgar");
      buburKacang=data.getBoolean("buburKacang");
      kuih=data.getBoolean("kuih");
      price=data.getBoolean("price");


      double price = 0;
     //StringBuffer stringBuffer = new StringBuffer();


      if(nsPutih==true)
      {

          tvOutput1.setText("Nasi Putih - RM 1.00");
          tvOutput1.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput1.setVisibility(View.GONE);
      }

      if (nsBeriyani==true)
      {
          tvOutput2.setText("Nasi Beriyani - RM 2.00");
          tvOutput2.setVisibility(View.VISIBLE);
          price+=2.0;
      }
      else
      {
          tvOutput2.setVisibility(View.GONE);
      }
      if (nsMinyak==true)
      {
          tvOutput3.setText("Nasi Minyak - RM 1.50");
          tvOutput3.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput3.setVisibility(View.GONE);
      }
      if(aymKurma==true)
      {
          tvOutput4.setText("Ayam Masak Kurma - RM 2.00");
          tvOutput4.setVisibility(View.VISIBLE);
          price+=2;
      }
      else
      {
          tvOutput4.setVisibility(View.GONE);
      }
      if (aymLemak==true)
      {
          tvOutput5.setText("Ayam Masak Lemak - RM 4.00");
          tvOutput5.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput5.setVisibility(View.GONE);
      }

      if (aymKari==true)
      {
          tvOutput6.setText("Ayam Masak Kari - RM 4.00");
          tvOutput6.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput6.setVisibility(View.GONE);
      }

      if (aymRendang==true)
      {
          tvOutput7.setText("Ayam Masak Rendang - RM 4.00");
          tvOutput7.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput7.setVisibility(View.GONE);
      }

      if (aymMerah==true)
      {
          tvOutput8.setText("Ayam Masak Merah - RM 4.00");
          tvOutput8.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput8.setVisibility(View.GONE);
      }

      if (prtAsam==true)
      {
          tvOutput9.setText("Perut Air Asam - RM 3.00");
          tvOutput9.setVisibility(View.VISIBLE);
          price+=3;
      }
      else
      {
          tvOutput9.setVisibility(View.GONE);
      }

      if (dggTomato==true)
      {
          tvOutput10.setText("Daging Masak Tomato - RM 4.00");
          tvOutput10.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput10.setVisibility(View.GONE);
      }

      if (dggSinggang==true)
      {
          tvOutput11.setText("Daging Masak Singgang - RM 4.00");
          tvOutput11.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput11.setVisibility(View.GONE);
      }

      if (dggMerah==true)
      {
          tvOutput12.setText("Daging Masak Merah - RM 4.00");
          tvOutput12.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput12.setVisibility(View.GONE);
      }

      if (kerabuTaugeh==true)
      {
          tvOutput13.setText("Kerabu Taugeh - RM 1.50");
          tvOutput13.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput13.setVisibility(View.GONE);
      }

      if (pindangKacang==true)
      {
          tvOutput14.setText("Pindang Kacang - RM 1.00");
          tvOutput14.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput14.setVisibility(View.GONE);
      }

      if (jelatah==true)
      {
          tvOutput15.setText("Jelatah - RM 2.00");
          tvOutput15.setVisibility(View.VISIBLE);
          price+=2;
      }
      else
      {
          tvOutput15.setVisibility(View.GONE);
      }

      if (dalcaSayur==true)
      {
          tvOutput16.setText("Dalca Sayur - RM 1.50");
          tvOutput16.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput16.setVisibility(View.GONE);
      }

      if (papadom==true)
      {
          tvOutput17.setText("Papadom - RM 0.50");
          tvOutput17.setVisibility(View.VISIBLE);
          price+=0.5;
      }
      else
      {
          tvOutput17.setVisibility(View.GONE);
      }

      if (kariIkan==true)
      {
          tvOutput18.setText("Kari Ikan - RM 1.50");
          tvOutput18.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput18.setVisibility(View.GONE);
      }

      if (ikanMasin==true)
      {
          tvOutput19.setText("Kari Ikan Masin - RM 4.00");
          tvOutput19.setVisibility(View.VISIBLE);
          price+=4;
      }
      else
      {
          tvOutput19.setVisibility(View.GONE);
      }

      if (sirapAis==true)
      {
          tvOutput20.setText("Minuman Sejuk - RM 1.50");
          tvOutput20.setVisibility(View.VISIBLE);
          price+=1.5;
      }
      else
      {
          tvOutput20.setVisibility(View.GONE);
      }

      if (oren==true)
      {
          tvOutput21.setText("Jus Buah - RM 2.00");
          tvOutput21.setVisibility(View.VISIBLE);
          price+=2;
      }
      else
      {
          tvOutput21.setVisibility(View.GONE);
      }

      if (tehTarik==true)
      {
          tvOutput22.setText("Minuman Panas - RM 1.00");
          tvOutput22.setVisibility(View.VISIBLE);
          price+=1.0;
      }
      else
      {
          tvOutput22.setVisibility(View.GONE);
      }

      if (agarAgar==true)
      {
          tvOutput23.setText("Agar Agar - RM 1.00");
          tvOutput23.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput23.setVisibility(View.GONE);
      }

      if (buburKacang==true)
      {
          tvOutput24.setText("Bubur Kacang - RM 2.50");
          tvOutput24.setVisibility(View.VISIBLE);
          price+=2.5;
      }
      else
      {
          tvOutput24.setVisibility(View.GONE);
      }

      if (kuih==true)
      {
          tvOutput25.setText("Kuih Muih - RM 1.00");
          tvOutput25.setVisibility(View.VISIBLE);
          price+=1;
      }
      else
      {
          tvOutput25.setVisibility(View.GONE);
      }

      Log.e("MENU PAKEJ A","Total Price: RM "+ price);
         String stringdouble = Double.toString(price);
         tvOutput26.setText("\n\n Price Per Person : RM "+ stringdouble);


         double e1 = Double.parseDouble(stringdouble.toString());
         double e2 = Double.parseDouble(phone.toString()); <!-- the error line -->

            tvOutput28.setText("Total price for all guest(s) : RM " + String.valueOf(e1*e2));

    }
public void goHome(View v){
    {
        Intent intent = new Intent(v.getContext(), MainActivity.class);
        startActivityForResult(intent,0);}
    }
public void goPackage(View v){
    {
        Intent intent = new Intent(v.getContext(), mcpakej1.class);
        startActivityForResult(intent,0);}
    }
public void goOrder(View v){
    {
        Intent intent = new Intent(v.getContext(), OrderProcess.class);
        startActivityForResult(intent,0);}
    }

 }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

tvOutput4.setText("Multiplication Result is: " +String.valueOf(e1*e2));
tvOutput4.setVisibility(View.VISIBLE);