IrishWhiskey
IrishWhiskey

Reputation: 339

setText in Fragment from MainActivity Android

So I'm trying to populate one fragment from another fragment with a button in my main activity. The main activity is the parent of both fragments. The layout looks like this

fragMoney contains TextViews that need to be set

fragCalculate contains EditText fields that i take input from to set fragMoney's TextViews and the button that calculates the totals.

MainActivity, the parent, is where I'm attempting to do this work.

I get a nice long error message about null pointer exception but I can't seem to figure out where I'm going wrong. I would venture a guess that its because I'm not declaring something before a certain piece of code. Anyways, here is the code and the logcat.

Main Activity Button

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");
}

Main Activity Methods

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

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

Upvotes: 0

Views: 719

Answers (1)

Aaron McIver
Aaron McIver

Reputation: 24713

The exception seems pretty clear to me, either packPrice or numPacks are null or the return from getText is null within your getDaily method.

You need to figure out which portion of the call is in fact null and work backwards from that.

Upvotes: 2

Related Questions