因此,我尝试在我的主要活动中使用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;
}
logcat的
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
一如既往地感谢大家给予的任何帮助
更新说明
好的,所以在调试后出现这行代码:
double price = Double.parseDouble(packPrice.getText().toString());
给我带来麻烦。 EditText
集合中的CalcFrag
字段采用类型numDecimals
,在此次运行中我输入了8.7
有什么想法吗?
第二次更新 这是初始化packPrice的代码,它在onCreate
中EditText packPrice = (EditText) findViewById(R.id.per_pack_Id);
第三次更新 这是我用log.v运行的代码,也见上面修改过的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;
}
当我在崩溃之前运行它时,log.v吐出了这个
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
答案 0 :(得分:2)
异常似乎很明显,packPrice
或numPacks
为空,或getText
的回复在getDaily
方法中为空。
您需要确定调用的哪个部分实际上是空的,然后从那里开始工作。