2015-09-13 38 views
0

這些按鈕根本不起作用,下面的結果。忽略代碼中的註釋:)。我複製/粘貼了需要縮短這個問題多長時間的XML的重要部分。不能在我的程序中找到一個錯誤(Android編程)

截圖應用的:http://i.imgur.com/DmrOEiM.png

,當我點擊「計算」按鈕也可以發送訂單按鈕不起作用或者它不工作。

XML

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/txtCheese" 
    android:inputType="number" 
    android:layout_alignTop="@+id/lblCheese" 
    android:layout_alignLeft="@+id/txtHam" 
    android:layout_alignStart="@+id/txtHam" 
    android:layout_alignRight="@+id/txtHam" 
    android:layout_alignEnd="@+id/txtHam" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/txtFries" 
    android:inputType="number" 
    android:layout_alignTop="@+id/lblFries" 
    android:layout_alignLeft="@+id/txtCheese" 
    android:layout_alignStart="@+id/txtCheese" 
    android:layout_alignRight="@+id/txtCheese" 
    android:layout_alignEnd="@+id/txtCheese" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/txtDrinks" 
    android:inputType="number" 
    android:layout_alignTop="@+id/lblDrinks" 
    android:layout_alignLeft="@+id/txtFries" 
    android:layout_alignStart="@+id/txtFries" 
    android:layout_alignRight="@+id/txtFries" 
    android:layout_alignEnd="@+id/txtFries" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Compute" 
    android:id="@+id/btnCompute" 
    android:onClick="doCompute" 
    android:layout_below="@+id/txtDrinks" 
    android:layout_alignLeft="@+id/proDrinks" 
    android:layout_alignStart="@+id/proDrinks" 
    android:layout_marginTop="32dp" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Send Orders" 
    android:id="@+id/btnSend" 
    android:onClick="doSend" 
    android:layout_alignBottom="@+id/btnCompute" 
    android:layout_centerHorizontal="true" /> 

JAVA

package com.example.administrator.jollymangdonald; 

import android.app.Activity; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import org.w3c.dom.Text; 


public class MainActivity extends Activity { 

Button btnCompute, btnSend; 
EditText txtHam, txtCheese, txtFries, txtDrinks; 
TextView lblHam, lblCheese, lblFries, lblDrinks, lblHam2, lblCheese2, lblFries2, lblDrinks2, lblTotal, productHam, productCheese, productFries, productDrinks, Total; 

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

    btnCompute = (Button) findViewById(R.id.btnCompute); 
    btnSend = (Button) findViewById(R.id.btnSend); 

    txtHam = (EditText) findViewById(R.id.txtHam); 
    txtCheese = (EditText) findViewById(R.id.txtCheese); 
    txtFries = (EditText) findViewById(R.id.txtFries); 
    txtDrinks = (EditText) findViewById(R.id.txtDrinks); 

    lblTotal = (TextView) findViewById(R.id.lblTotal); 
    lblHam = (TextView) findViewById(R.id.lblHam); 
    lblCheese = (TextView) findViewById(R.id.lblCheese); 
    lblFries = (TextView) findViewById(R.id.lblFries); 
    lblDrinks = (TextView) findViewById(R.id.lblDrinks); 

    lblHam2 = (TextView) findViewById(R.id.lblHam2); 
    lblCheese2 = (TextView) findViewById(R.id.lblCheese2); 
    lblFries2 = (TextView) findViewById(R.id.lblFries2); 
    lblDrinks = (TextView) findViewById(R.id.lblDrinks); 

    productHam = (TextView) findViewById(R.id.proHam); 
    productCheese = (TextView) findViewById(R.id.proCheese); 
    productFries = (TextView) findViewById(R.id.proFries); 
    productDrinks = (TextView) findViewById(R.id.proDrinks); 


    btnSend.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SendSMSMessage(); 
     } 
    }); 
    btnCompute.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      doCompute(); 
     } 
    });} 



public void doCompute(){ 

    int lblHam = 25; 
    int lblCheese = 35; 
    int lblFries = 25; 
    int lblDrinks = 20; 

    int Total; 

    int HamQ = Integer.valueOf(txtHam.getText().toString()); // unnecessary .toString(), getText() already returns string, Oops, editable pala return, sorry, haha 
    int CheeseQ = Integer.valueOf(txtCheese.getText().toString()); 
    int FriesQ = Integer.valueOf(txtFries.getText().toString()); 
    int DrinksQ = Integer.valueOf(txtDrinks.getText().toString()); 

    int HamSub; 
    int CheeseSub; 
    int FriesSub; 
    int DrinksSub; 

    HamSub = lblHam * HamQ; 
    CheeseSub = lblCheese * CheeseQ; 
    FriesSub = lblFries * FriesQ; 
    DrinksSub = lblDrinks * DrinksQ; 

    lblHam2.setText(HamSub + ""); // you can also do it this way, much more concise 
    lblCheese2.setText(Integer.toString(CheeseSub)); 
    lblFries2.setText(Integer.toString(FriesSub)); 
    lblDrinks2.setText(Integer.toString(FriesSub)); 

    Total = HamSub + CheeseSub + FriesSub + DrinksSub; 

    lblTotal.setText(Integer.toString(Total)); 
} 

public void SendSMSMessage(){ 
    Log.i("Send SMS", ""); 
    String phoneNo = "example phone number"; 
    String msg = productHam.getText().toString() + " " + txtHam.getText().toString() + " " + lblHam2.getText().toString() + "\n" + productCheese.getText().toString() + " " + 
      txtCheese.getText().toString() + " " + lblCheese2.getText().toString() + "\n" + productFries.getText().toString() + " " + txtFries.getText().toString() + " " + lblFries2.getText().toString() + "\n" + 
      productDrinks.getText().toString() + " " + txtDrinks.getText().toString() + " " + lblDrinks2.getText().toString() + "\n" + lblTotal.getText().toString(); 

    try { 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage(phoneNo, null, msg, null, null); 
     Toast.makeText(getApplicationContext(), "SMS Sent", Toast.LENGTH_LONG).show(); 
    } 

    catch (Exception e){ 
     Toast.makeText(getApplicationContext(), "SMS Failed, Try Again", Toast.LENGTH_LONG).show(); 
     e.printStackTrace(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, 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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

錯誤: 對於計算:http://i.imgur.com/Z4FuieL.png 對於發送訂單:http://i.imgur.com/KCFe1Ne.png

+1

你試過調試嗎? – buzeeg

+0

你能告訴我們第96行和第108行是什麼嗎?那些是您發生異常的地點。 – GiantTree

+0

我試過但我找不到 – opperska

回答

1

這是因爲lblDrinks2永遠不會初始化。

如圖所示由第一張截圖,您嘗試調用一個方法上lbDrinks2是空(線96):

public void doCompute(){ 
    // ... 
    lblDrinks2.setText(Integer.toString(FriesSub)); 
    // ... 
} 

在這裏是同樣的問題(第108行):

public void SendSMSMessage(){ 
    // ... 
    productDrinks.getText().toString() + " " + txtDrinks.getText().toString() + " " + lblDrinks2.getText().toString() + "\n" + lblTotal.getText().toString(); 
    // ... 
} 

嘗試在onCreate方法中初始化它:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // ... 
    lblDrinks2 = (TextView) findViewById(R.id.lblDrinks2); // if R.id.lblDrinks2 exists 
    // ... 
} 
+0

謝謝,它的工作!從現在開始,即時將集中我的眼睛在我的代碼:( lbldrinks是從oncreate重複這就是爲什麼我得到錯誤 它應該是lbldrinks和lbldrinks2在oncreate方法:)謝謝你很多 – opperska

+0

不客氣,只是不要忘記關閉這個問題:)下一次小建議,嘗試在引發錯誤的行上放置一個斷點,並在運行時對其進行評估。這將幫助您調查問題。 –

相關問題