首先,我只想说,stackoverflow 社区至少可以这么说。尝试构建我的前两个应用程序,目前已部署但试图在非洲淘汰几个课程。
所以,我正在制作一个非常基本的蛋白质跟踪应用程序。原谅我过多的评论,当我试图弄清楚这些东西时,它让我保持头脑清醒。
我已经查找并找到了很多以前问题的答案,但我不知道如何在这方面取得进展。
我从那个开始,它很棒(谢谢你 luvieere),但我认为我制作的可编辑部分有点吓人。我在 Eclipse 中的 logcat 正在调用该行以及我的计算方法。
这可能只是一些简单的事情,但据我所见,这是一个如此广泛的错误,你只需要查看这些可怕的代码行就可以知道我哪里出错了。再次感谢人们的帮助,这是我第一次提出问题,所以让我知道如何给你疯狂的代表。:D
一些代码:
public class anitasProteinTracker extends Activity implements OnClickListener {
Button btnOverviewSnapshot, btnNomNoms, btnSettingsCalculate;
EditText etSettingsProteinGoal, etWeight;
<//random code here//>
btnSettingsCalculate = (Button) findViewById(R.id.btnSettingsCalculate);
btnSettingsCalculate.setOnClickListener(this);
<//random code here//>
public void onClick(View v) {
switch(v.getId()){
<//random code here//>
case R.id.btnSettingsCalculate:
//clicking this will find the value and set it to the goal edittext
calcProteinGoal(); <---This line showed up in logcat
break;
}
}
private void calcProteinGoal() {
// TODO Auto-generated method stub
Editable etWeightlbs; <----this is the other error line in logcat
double weightlbs = 0;
double weightkgs = 0;
double proteinMultiplier;
double gProtein = 0;
//get the weight from the user and ensure it's a number
etWeightlbs = etWeight.getText();
if (etWeightlbs != null)
weightlbs = Double.parseDouble(etWeightlbs.toString());
//convert lbs -> kgs
weightkgs = weightlbs / 2.2;
//get spinner value
//don't know how to do that yet, going to set the variable
proteinMultiplier = 0.8;
//set up protein goal
gProtein = weightkgs * proteinMultiplier;
etSettingsProteinGoal.setText(String.valueOf(gProtein));
一些logcat:
07-03 21:41:14.405: W/dalvikvm(1861): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0)
07-03 21:41:14.429: E/AndroidRuntime(1861): FATAL EXCEPTION: main
07-03 21:41:14.429: E/AndroidRuntime(1861): java.lang.NullPointerException
07-03 21:41:14.429: E/AndroidRuntime(1861): at com.joemac.anitasprotein.anitasProteinTracker.calcProteinGoal(anitasProteinTracker.java:119)
07-03 21:41:14.429: E/AndroidRuntime(1861): at com.joemac.anitasprotein.anitasProteinTracker.onClick(anitasProteinTracker.java:102)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.view.View.performClick(View.java:2408)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.view.View$PerformClick.run(View.java:8816)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.os.Handler.handleCallback(Handler.java:587)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.os.Looper.loop(Looper.java:123)
07-03 21:41:14.429: E/AndroidRuntime(1861): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-03 21:41:14.429: E/AndroidRuntime(1861): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 21:41:14.429: E/AndroidRuntime(1861): at java.lang.reflect.Method.invoke(Method.java:521)
07-03 21:41:14.429: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-03 21:41:14.429: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-03 21:41:14.429: E/AndroidRuntime(1861): at dalvik.system.NativeStart.main(Native Method)