2016-04-09 72 views
0

所以我創建了一個應用程序來計算食品成本,但是當我按下按鈕來帶我到計算應用程序崩潰的活動。該應用程序將改變罰款,直到我添加的方法來做實際的計算。我是新來的,所以我意識到代碼可能有點混亂。應用程序崩潰啓動其他活動

發射活動:

package com.example.foodcostcalculator; 

import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.app.Activity; 

public class ActivityHome extends Activity { 

private TextView mMainHeader; 
private Button mStartCalc; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_activity_home); 


    mStartCalc = (Button)findViewById(R.id.calc_button); 
    mStartCalc.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Intent intent1 = new Intent(ActivityHome.this, StartCalc.class); 
      if (intent1 != null) { 
       startActivity(intent1); 
      } 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_home, menu); 
    return true; 


} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

StartCalc活動:

package com.example.foodcostcalculator; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class StartCalc extends Activity { 

private Button mBackButton; 
private EditText mItemName; 
private EditText mItemCost; 
private EditText mSellPercent; 
private EditText mSellAmount; 
private Button mCalculate; 

String item = mItemName.getText().toString();          
String itcost = mItemCost.getText().toString();          
int cost = Integer.parseInt(itcost); 
String sellperc = mSellPercent.getText().toString();          
int no2 = Integer.parseInt(sellperc); 
String sellamou = mSellAmount.getText().toString();        
int no3 = Integer.parseInt(sellamou); 

private String name1 = "Menu Item: $" + item; 
private String cost1 = "Item Cost: $" + cost; 
private String perc1 = "Sell Percent: %" + no2; 
private String amount1 = "Sell Price: $" + no3; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start_calc); 

    final AlertDialog.Builder builder2 = new AlertDialog.Builder(this); 
    builder2.setMessage("Required Fields aren't filled!"); 
    builder2.setCancelable(true); 




    mBackButton = (Button)findViewById(R.id.back_button1); 
    mBackButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Intent intent1 = new Intent(StartCalc.this, ActivityHome.class); 
      if (intent1 != null) { 
       startActivity(intent1); 
      } 
     } 
    }); 

    mItemName = (EditText)findViewById(R.id.edit_item_name); 
    mItemCost = (EditText)findViewById(R.id.item_cost); 
    mSellPercent = (EditText)findViewById(R.id.profit_percent); 
    mSellAmount = (EditText)findViewById(R.id.profit_amount); 

    mCalculate = (Button)findViewById(R.id.calculate_button); 
    mCalculate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if (mSellPercent.getText().toString().trim().length() == 0) { 
       int no5 = Calculator.getPerc(cost, no3); 
       String percstring = "Item Percent: %" + no5; 
       final AlertDialog.Builder builder1 = new AlertDialog.Builder(StartCalc.this); 
       builder1.setMessage(name1 + "\n" + cost + "\n" + percstring + "\n" + amount1); 
       builder1.setCancelable(true); 
       AlertDialog alert11 = builder1.create(); 
       alert11.show(); 
      } 
      else if (mSellAmount.getText().toString().trim().length() == 0) { 
       int no6 = Calculator.getPerc(cost, no3); 
       String amountstring = "Item Amount: $" + no6; 
       final AlertDialog.Builder builder1 = new AlertDialog.Builder(StartCalc.this); 
       builder1.setMessage(name1 + "\n" + cost1 + "\n" + perc1 + "\n" + amountstring); 
       builder1.setCancelable(true); 
       AlertDialog alert11 = builder1.create(); 
       alert11.show(); 
      } 

      else { 
       AlertDialog alert2 = builder2.create(); 
       alert2.show(); 
      } 
     } 
    }); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.start_calc, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

計算器Java類:

package com.example.foodcostcalculator; 

public class Calculator { 
    private static int cost3; 
    private static int percent; 
    private static int amountsell; 

    public static void setCost(int cd) { 
     cost3 = cd; 
    } 

    public static int getCost() { 
     return cost3; 
    } 

    public static void setPerc(int p) { 
     percent = p; 
    } 

    public static void setAmount(int am) { 
     amountsell = am; 
    } 

    public static int getPerc(int a, int b) { 

     percent = (b/a) * 100; 
     return percent; 
    } 

    public static int getAmount(int c, int e) { 
     amountsell = c * e; 
     return amountsell; 
    } 
} 

登錄:

04-09 01:34:37.812: E/AndroidRuntime(14908): FATAL EXCEPTION: main 
04-09 01:34:37.812: E/AndroidRuntime(14908): Process: com.example.foodcostcalculator, PID: 14908 
04-09 01:34:37.812: E/AndroidRuntime(14908): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.foodcostcalculator/com.example.foodcostcalculator.StartCalc}: java.lang.NullPointerException 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread.access$800(ActivityThread.java:139) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.os.Handler.dispatchMessage(Handler.java:102) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.os.Looper.loop(Looper.java:136) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread.main(ActivityThread.java:5097) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at java.lang.reflect.Method.invokeNative(Native Method) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at java.lang.reflect.Method.invoke(Method.java:515) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at dalvik.system.NativeStart.main(Native Method) 
04-09 01:34:37.812: E/AndroidRuntime(14908): Caused by: java.lang.NullPointerException 
04-09 01:34:37.812: E/AndroidRuntime(14908): at com.example.foodcostcalculator.StartCalc.<init>(StartCalc.java:22) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at java.lang.Class.newInstanceImpl(Native Method) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at java.lang.Class.newInstance(Class.java:1208) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.Instrumentation.newActivity(Instrumentation.java:1084) 
04-09 01:34:37.812: E/AndroidRuntime(14908): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115) 
04-09 01:34:37.812: E/AndroidRuntime(14908): ... 11 more 

回答

0

StartCalc活動中,以下聲明默認初始化爲null

private Button mBackButton; 
private EditText mItemName; 
private EditText mItemCost; 
private EditText mSellPercent; 
private EditText mSellAmount; 
private Button mCalculate; 

所以,當你嘗試做:

String item = mItemName.getText().toString();          
String itcost = mItemCost.getText().toString();          
int cost = Integer.parseInt(itcost); 
String sellperc = mSellPercent.getText().toString();          
int no2 = Integer.parseInt(sellperc); 
String sellamou = mSellAmount.getText().toString();        
int no3 = Integer.parseInt(sellamou); 

的Android拋出一個java.lang.NullPointerException。請注意,活動代碼中的View組件與佈局之間的連接是,只有在通過findViewById()方法鏈接後才能建立。

在撥打findviewById()後寫上面的代碼,如下所示。

... 
mItemName = (EditText)findViewById(R.id.edit_item_name); 
mItemCost = (EditText)findViewById(R.id.item_cost); 
mSellPercent = (EditText)findViewById(R.id.profit_percent); 
mSellAmount = (EditText)findViewById(R.id.profit_amount); 

mCalculate = (Button)findViewById(R.id.calculate_button); 
item = mItemName.getText().toString();          
itcost = mItemCost.getText().toString();          
cost = Integer.parseInt(itcost); 
sellperc = mSellPercent.getText().toString();          
no2 = Integer.parseInt(sellperc); 
sellamou = mSellAmount.getText().toString();        
no3 = Integer.parseInt(sellamou); 
+0

所以我得到了它能夠去到下一個活動做你說什麼,但現在當我按下計算崩潰。 – NCavins

+0

錯誤信息是什麼意思? –

+0

應用程序崩潰沒有錯誤消息。我將編輯日誌。 – NCavins

0
String item = mItemName.getText().toString();          
String itcost = mItemCost.getText().toString(); 
String sellperc = mSellPercent.getText().toString(); 
String sellamou = mSellAmount.getText().toString(); 

mItemNamemItemCostmSellPercentmSellAmountnull,您無法訪問自己的方法。只要使用findViewById方法初始化它們,就可以訪問它們。 在你StartCalc活動,把這個代碼:mItemNamemItemCostmSellPercentmSellAmount被itialized

String item = mItemName.getText().toString();          
String itcost = mItemCost.getText().toString();          
int cost = Integer.parseInt(itcost); 
String sellperc = mSellPercent.getText().toString();          
int no2 = Integer.parseInt(sellperc); 
String sellamou = mSellAmount.getText().toString();        
int no3 = Integer.parseInt(sellamou); 

private String name1 = "Menu Item: $" + item; 
private String cost1 = "Item Cost: $" + cost; 
private String perc1 = "Sell Percent: %" + no2; 
private String amount1 = "Sell Price: $" + no3; 

mItemName = (EditText)findViewById(R.id.edit_item_name); 
mItemCost = (EditText)findViewById(R.id.item_cost); 
mSellPercent = (EditText)findViewById(R.id.profit_percent); 
mSellAmount = (EditText)findViewById(R.id.profit_amount); 

這樣的:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_start_calc); 

    final AlertDialog.Builder builder2 = new AlertDialog.Builder(this); 
    builder2.setMessage("Required Fields aren't filled!"); 
    builder2.setCancelable(true); 

    mBackButton = (Button)findViewById(R.id.back_button1); 
    mBackButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Intent intent1 = new Intent(StartCalc.this, ActivityHome.class); 
      if (intent1 != null) { 
       startActivity(intent1); 
      } 
     } 
    }); 

    mItemName = (EditText)findViewById(R.id.edit_item_name); 
    mItemCost = (EditText)findViewById(R.id.item_cost); 
    mSellPercent = (EditText)findViewById(R.id.profit_percent); 
    mSellAmount = (EditText)findViewById(R.id.profit_amount); 

    String item = mItemName.getText().toString();          
    String itcost = mItemCost.getText().toString();          
    int cost = Integer.parseInt(itcost); 
    String sellperc = mSellPercent.getText().toString();          
    int no2 = Integer.parseInt(sellperc); 
    String sellamou = mSellAmount.getText().toString();        
    int no3 = Integer.parseInt(sellamou); 

    String name1 = "Menu Item: $" + item; 
    String cost1 = "Item Cost: $" + cost; 
    String perc1 = "Sell Percent: %" + no2; 
    String amount1 = "Sell Price: $" + no3; 
    mCalculate = (Button)findViewById(R.id.calculate_button); 
+0

我真的不知道你這是什麼意思@Đăng誇HUYNH – NCavins

0

的問題是在你的StartCalc活動,當它第一次實例化,它初始化可變itemitcostcostsellperc到依賴於mItemNamemItemCostmSellPercentmSellAmount值,其是空的在該點(直到onCreate()被調用)。

您可以嘗試移動這些分配操作onCreate(),你賦值mItemName後,mItemCostmSellPercentmSellAmount得到了異常(取決於你正在嘗試做的)。但請記住,mItemName EditText發生更改時,item(和其他變量)的值不會更改。所以,我認爲在使用它們之前你最好分配這些值。