可编辑/方法调用抛出java.lang.nullpointerexception?

时间:2012-07-03 19:20:12

标签: android nullpointerexception android-edittext

首先,我只是想说stackoverflow社区能够说得最少。试图建立我的前两个应用程序,目前已经部署,但试图在非洲期间淘汰几个类。

所以,我正在制作一个非常基本的蛋白质跟踪应用程序。请原谅我过多的评论,因为我试图弄清楚这些东西,所以我会保持头脑清醒。

我抬头看了很多以前问题的答案,但我不知道如何推进这个问题。

How to calculate EditText value in Android?

我从那开始,它很棒(谢谢你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)

1 个答案:

答案 0 :(得分:0)

只是因为我没有在那里看到它...你正在为EditText做findViewById吗?我看到的只是按钮的绑定。