我有一个多类项目。这是作业:创建一个允许输入信息以订购包裹递送的应用程序。我必须有两个额外的类,并将信息从一个传递到另一个,但我不知道为什么应用程序想要崩溃甚至看看代码是否可行。我的导师没有太多运气看着它,但他很忙,我决定看看其他人是否可以看一眼。提前谢谢!
MainActivity.Java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
parcel parcelObject;
address addressObject;
EditText startAdd;
EditText startZip;
EditText endAdd;
EditText endZip;
EditText parcelWeight;
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
//addressObject = new address(this);
parcelObject = new parcel(this);
}
public void buttonOnClick(View v ) {
Button button=(Button) v;
TextView textView = (TextView) findViewById(R.id.shippingTotal);
parcelObject.shipping();
textView.setText("Your Shipping Total is: $ " + parcelObject.sCost);
}
@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);
}
}
address.java
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Cyn on 10/16/2015.
*/
public class address {
parcel parcelObject;
EditText startAdd;
EditText startZip;
EditText endAdd;
EditText endZip;
double sZip, eZip;
public address (Activity activity) {
startAdd = (EditText)activity.findViewById(R.id.originZip);
startZip = (EditText)activity.findViewById(R.id.originZip);
endAdd = (EditText)activity.findViewById(R.id.destAdd);
endZip = (EditText)activity.findViewById(R.id.destZip);
sZip = Double.parseDouble(startZip.getText().toString());
eZip = Double.parseDouble(endAdd.getText().toString());
}
}
和 parcel.java
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by Cyn on 10/16/2015.
*/
public class parcel {
address addressObject;
EditText startAdd;
EditText startZip;
EditText endAdd;
EditText endZip;
EditText parcelWeight;
double sZip, eZip, pWeight, sCost;
public parcel(Activity activity) {
addressObject = new address(activity);
startAdd = addressObject.startAdd;
endAdd = addressObject.endAdd;
endZip = addressObject.endZip;
sZip = addressObject.sZip;
eZip = addressObject.eZip;
parcelWeight = (EditText)activity.findViewById(R.id.packageWeight);
pWeight = Double.parseDouble(parcelWeight.getText().toString());
}
public void shipping() {
if (sZip == eZip) {
sCost = pWeight * 1;
} else {
sCost = pWeight * 2;
}
}
}
android监视器的错误部分在哪里:
11-20 01:27:53.768 1444-1444/cyn.assignment4 D/AndroidRuntime: Shutting down VM
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: FATAL EXCEPTION: main
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: Process: cyn.assignment4, PID: 1444
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{cyn.assignment4/cyn.assignment4.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: Caused by: java.lang.NumberFormatException: Invalid double: ""
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at java.lang.StringToReal.parseDouble(StringToReal.java:267)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at java.lang.Double.parseDouble(Double.java:301)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at cyn.assignment4.address.<init>(address.java:33)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at cyn.assignment4.parcel.<init>(parcel.java:22)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at cyn.assignment4.MainActivity.onCreate(MainActivity.java:35)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-20 01:27:53.772 1444-1444/cyn.assignment4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-20 01:28:36.701 1444-1451/cyn.assignment4 W/art: Suspending all threads took: 135.053ms
11-20 01:30:38.025 1444-1451/cyn.assignment4 W/art: Suspending all threads took: 218.758ms
11-20 01:31:17.505 1444-1444/cyn.assignment4 I/Process: Sending signal. PID: 1444 SIG: 9
答案 0 :(得分:0)
sZip = Double.parseDouble(startZip.getText().toString());
eZip = Double.parseDouble(endAdd.getText().toString());
就是这样。您正尝试从double
解析String
,String
为空。首先使用
if(!startZip.getText().toString().isEmpty&& startZip.getText()!=null){
sZip = Double.parseDouble(startZip.getText().toString());
}
if(!endAdd.getText().toString().isEmpty&&endAdd.getText()!=null)...
答案 1 :(得分:0)
尽管答案部分正确,但两项小改动使代码工作正常。一个是简单的拼写错误修复(我没有抓到)。
首先,主要活动的变化:我们将“parcelObject = new parcel(this);从onCreate移动到buttonOnClick的顶部
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
//addressObject = new address(this);
}
public void buttonOnClick(View v ) {
parcelObject = new parcel(this);
Button button=(Button) v;
TextView textView = (TextView) findViewById(R.id.shippingTotal);
parcelObject.shipping();
textView.setText("Your Shipping Total is: $ " + parcelObject.sCost);
}
那么小错字就是:
sZip = Double.parseDouble(startZip.getText().toString());
eZip = Double.parseDouble(endAdd.getText().toString())
它应该是:
sZip = Double.parseDouble(startZip.getText().toString());
eZip = Double.parseDouble(endZip.getText().toString())
但是为了更多地解释代码,这是最终的address.java代码:
package cyn.assignment4;
import android.text.TextUtils;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by Cyn on 10/16/2015.
*/
public class address {
parcel parcelObject;
EditText startAdd;
EditText startZip;
EditText endAdd;
EditText endZip;
double sZip, eZip;
public address (Activity activity) {
startAdd = (EditText)activity.findViewById(R.id.originZip);
startZip = (EditText)activity.findViewById(R.id.originZip);
endAdd = (EditText)activity.findViewById(R.id.destAdd);
endZip = (EditText)activity.findViewById(R.id.destZip);
String checkUserInput = startZip.getText().toString();
if (TextUtils.isEmpty(checkUserInput)) {
startZip.setError("You must enter a valid zip code");
}
else {
sZip = Double.parseDouble(startZip.getText().toString());
}
String checkUserInput2 = endZip.getText().toString();
if (TextUtils.isEmpty(checkUserInput2)) {
endZip.setError("You must enter a valid zip code!");
}
else {
eZip = Double.parseDouble(endZip.getText().toString());
}
}
因此,主要活动中的小动作机会和应用程序就像魅力一样:)。