這些按鈕根本不起作用,下面的結果。忽略代碼中的註釋:)。我複製/粘貼了需要縮短這個問題多長時間的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
你試過調試嗎? – buzeeg
你能告訴我們第96行和第108行是什麼嗎?那些是您發生異常的地點。 – GiantTree
我試過但我找不到 – opperska