所以我试图在我的主要活动中用一个来填充fragment
另一个。主要活动是两者的父母。布局看起来像这样fragment
button
fragments
fragMoney
包含TextViews
需要设置的
fragCalculate
包含EditText
我从中获取输入以设置 fragMoney 的字段TextViews
以及button
计算总数的字段。
MainActivity
,父母,是我试图做这项工作的地方。
我收到一条很长的错误消息,null pointer exception
但我似乎无法弄清楚我哪里出错了。我冒昧地猜测这是因为我没有在某段代码之前声明某些内容。无论如何,这里是代码和 logcat。
主要活动按钮
public void calculateHandler(View view) {
Log.v("*** DEBUG ***", "calc button");
dailyView.setText("" + getDaily());
Log.v("*** DEBUG ***", "getting daily");
monthlyView.setText("" + getMonthly());
Log.v("*** DEBUG ***", "getting month");
annualView.setText("" + getAnnually());
Log.v("*** DEBUG ***", "getting year");
totalView.setText("" + getTotal());
Log.v("*** DEBUG ***", "getting total");
}
主要活动方式
public double getDaily() {
double price = Double.parseDouble(packPrice.getText().toString());
double packs = Double.parseDouble(numPacks.getText().toString());
daily = price * packs;
return daily;
}
public double getMonthly() {
double price = Double.parseDouble(packPrice.getText().toString());
double packs = Double.parseDouble(numPacks.getText().toString());
monthly = price * packs * 30.41;
return monthly;
}
public double getAnnually() {
double price = Double.parseDouble(packPrice.getText().toString());
double packs = Double.parseDouble(numPacks.getText().toString());
annually = (price * packs) * 365;
return annually;
}
public double getTotal () {
double price = Double.parseDouble(packPrice.getText().toString());
double packs = Double.parseDouble(numPacks.getText().toString());
double years = Double.parseDouble(numYears.getText().toString());
total = (price * packs) * 365 * years;
return total;
}
日志猫
03-30 16:27:43.443: E/AndroidRuntime(815): FATAL EXCEPTION: main
03-30 16:27:43.443: E/AndroidRuntime(815): java.lang.IllegalStateException: Could not execute method of the activity
03-30 16:27:43.443: E/AndroidRuntime(815): at android.view.View$1.onClick(View.java:3591)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.view.View.performClick(View.java:4084)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.view.View$PerformClick.run(View.java:16966)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.os.Handler.handleCallback(Handler.java:615)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.os.Looper.loop(Looper.java:137)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-30 16:27:43.443: E/AndroidRuntime(815): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 16:27:43.443: E/AndroidRuntime(815): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:27:43.443: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-30 16:27:43.443: E/AndroidRuntime(815): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-30 16:27:43.443: E/AndroidRuntime(815): at dalvik.system.NativeStart.main(Native Method)
03-30 16:27:43.443: E/AndroidRuntime(815): Caused by: java.lang.reflect.InvocationTargetException
03-30 16:27:43.443: E/AndroidRuntime(815): at java.lang.reflect.Method.invokeNative(Native Method)
03-30 16:27:43.443: E/AndroidRuntime(815): at java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:27:43.443: E/AndroidRuntime(815): at android.view.View$1.onClick(View.java:3586)
03-30 16:27:43.443: E/AndroidRuntime(815): ... 11 more
03-30 16:27:43.443: E/AndroidRuntime(815): Caused by: java.lang.NullPointerException
03-30 16:27:43.443: E/AndroidRuntime(815): at com.example.smokin4tomsullivan.MainActivity.getDaily(MainActivity.java:162)
03-30 16:27:43.443: E/AndroidRuntime(815): at com.example.smokin4tomsullivan.MainActivity.calculateHandler(MainActivity.java:199)
03-30 16:27:43.443: E/AndroidRuntime(815): ... 14 more
As always thank you everyone for any help given
Update Note
ok so after debugging it appears this line of code:
double price = Double.parseDouble(packPrice.getText().toString());
is giving me the trouble. It's an EditText
field in the CalcFrag
set to take the type numDecimals
, to which at this run i entered 8.7
Any ideas?
2nd Update This is the code that initialized packPrice, its in onCreate
EditText packPrice = (EditText) findViewById(R.id.per_pack_Id);
3rd Update this is the code I am running with log.v also see above for modified calcButton
public double getDaily() {
Log.v("*** DEBUG ***", "running getDaily");
EditText packPrice = (EditText) findViewById(R.id.per_pack_Id);
Log.v("*** DEBUG ***", "initialized packPrice");
double price = Double.parseDouble(packPrice.getText().toString());
Log.v("*** DEBUG ***", packPrice.getText().toString());
String test = ("" + price);
Log.v("*** DEBUG ***", test);
double packs = Double.parseDouble(numPacks.getText().toString());
String test1 = ("" + packs);
Log.v("*** DEBUG ***", test1);
daily = price * packs;
return daily;
}
When I run it before it crashes log.v spits out this
03-30 17:34:15.883: V/*** DEBUG ***(1166): calc button
03-30 17:34:15.883: V/*** DEBUG ***(1166): running getDaily
03-30 17:34:15.893: V/*** DEBUG ***(1166): initialized packPrice
03-30 17:34:15.893: V/*** DEBUG ***(1166): 9.9 <-- the values I entered
03-30 17:34:15.893: V/*** DEBUG ***(1166): 9.9 <-- the values I entered