按钮根本不起作用,结果如下。忽略代码中的注释:)。我复制/粘贴了缩短这个问题所需时间的XML的重要部分。
该应用的屏幕截图: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
答案 0 :(得分:1)
这是因为lblDrinks2
永远不会被初始化。
如第一个屏幕截图所示,您尝试在lbDrinks2
上调用一个null的方法(第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
// ...
}