从MainActivity在Android的碎片的setText [英] setText in Fragment from MainActivity Android
问题描述
所以我想填充有一个片段
从另一个片段
与按钮
在我的主要活动。主要活动是既片段父
。
布局看起来像这样
fragMoney
包含 TextViews
需要设置
fragCalculate
包含我需要输入从设置fragMoney的 TextViews
的EditText
字段code>和按钮
的计算总量。
MainActivity
,父,就是我试图做这项工作。
我得到关于空指针异常
有较长的错误信息,但我似乎无法弄清楚我要去哪里错了。我冒昧猜测其因为我不宣布某一段code之前的东西。不管怎么说,这里是code和logcat的。
主要活动按钮
公共无效calculateHandler(查看视图){
Log.v(*** DEBUG ***,计算按钮);
dailyView.setText(+ getDaily());
Log.v(*** DEBUG ***,经常得到);
monthlyView.setText(+ getMonthly());
Log.v(*** DEBUG ***,让月);
annualView.setText(+ getAnnually());
Log.v(*** DEBUG ***,获得今年);
totalView.setText(+ getTotal());
Log.v(*** DEBUG ***,让总);
}
主要活性的方法
公共双getDaily(){ 双价= Double.parseDouble(packPrice.getText()的toString());
双包= Double.parseDouble(numPacks.getText()的toString()); 日常=价格*包; 日收益率;
}公共双getMonthly(){
双价= Double.parseDouble(packPrice.getText()的toString());
双包= Double.parseDouble(numPacks.getText()的toString()); 每月=价格*包* 30.41; 每月的回报;
}公共双getAnnually(){
双价= Double.parseDouble(packPrice.getText()的toString());
双包= Double.parseDouble(numPacks.getText()的toString()); 每年=(价格*包)* 365; 每年返回;
}公共双getTotal(){
双价= Double.parseDouble(packPrice.getText()的toString());
双包= Double.parseDouble(numPacks.getText()的toString());
双年= Double.parseDouble(numYears.getText()的toString()); 总=(价格*包)* 365 *年; 总回报;
}
的logcat
03-30 16:27:43.443:E / AndroidRuntime(815):致命异常:主要
03-30 16:27:43.443:E / AndroidRuntime(815):java.lang.IllegalStateException:无法执行活动的方法
03-30 16:27:43.443:E / AndroidRuntime(815):在android.view.View $ 1.onClick(View.java:3591)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.view.View.performClick(View.java:4084)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.view.View $ PerformClick.run(View.java:16966)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.os.Handler.handleCallback(Handler.java:615)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.os.Handler.dispatchMessage(Handler.java:92)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.os.Looper.loop(Looper.java:137)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.app.ActivityThread.main(ActivityThread.java:4745)
03-30 16:27:43.443:E / AndroidRuntime(815):在java.lang.reflect.Method.invokeNative(本机方法)
03-30 16:27:43.443:E / AndroidRuntime(815):在java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:27:43.443:E / AndroidRuntime(815):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
03-30 16:27:43.443:E / AndroidRuntime(815):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-30 16:27:43.443:E / AndroidRuntime(815):在dalvik.system.NativeStart.main(本机方法)
03-30 16:27:43.443:E / AndroidRuntime(815):由:java.lang.reflect.InvocationTargetException
03-30 16:27:43.443:E / AndroidRuntime(815):在java.lang.reflect.Method.invokeNative(本机方法)
03-30 16:27:43.443:E / AndroidRuntime(815):在java.lang.reflect.Method.invoke(Method.java:511)
03-30 16:27:43.443:E / AndroidRuntime(815):在android.view.View $ 1.onClick(View.java:3586)
03-30 16:27:43.443:E / AndroidRuntime(815):11 ...更多
03-30 16:27:43.443:E / AndroidRuntime(815):致:显示java.lang.NullPointerException
03-30 16:27:43.443:E / AndroidRuntime(815):在com.example.smokin4tomsullivan.MainActivity.getDaily(MainActivity.java:162)
03-30 16:27:43.443:E / AndroidRuntime(815):在com.example.smokin4tomsullivan.MainActivity.calculateHandler(MainActivity.java:199)
03-30 16:27:43.443:E / AndroidRuntime(815):14 ...更多
一如往常谢谢大家给予任何帮助。
更新注意
确定,所以看来这行code的调试后:
双价= Double.parseDouble(packPrice.getText()的toString());
是给我的麻烦。这是在 CalcFrag
设置采取键入 numDecimals $ C $的
的EditText
字段C>,到目前来看,我进入8.7
任何想法?
2日更新
这是code,它初始化packPrice,其的onCreate
的EditText packPrice =(EditText上)findViewById(R.id.per_pack_Id);
3日更新
这是code我与log.v运行也见上述修改calcButton
公共双getDaily(){
Log.v(*** DEBUG ***,运行getDaily);
的EditText packPrice =(EditText上)findViewById(R.id.per_pack_Id);
Log.v(*** DEBUG ***,packPrice初始化);
双价= Double.parseDouble(packPrice.getText()的toString());
Log.v(*** DEBUG ***,packPrice.getText()的toString());
字符串测试=(+价格);
Log.v(*** DEBUG ***,测试);
双包= Double.parseDouble(numPacks.getText()的toString());
字符串TEST1 =(+包);
Log.v(*** DEBUG ***,测试1); 日常=价格*包; 日收益率;
}
当我运行它,它崩溃log.v吐出了在此之前
03-30 17:34:15.883:V / *** *** DEBUG(1166):计算按钮
03-30 17:34:15.883:V / *** *** DEBUG(1166):运行getDaily
03-30 17:34:15.893:V / *** *** DEBUG(1166):初始化packPrice
03-30 17:34:15.893:V / *** *** DEBUG(1166):9.9< - 我输入的值
03-30 17:34:15.893:V / *** *** DEBUG(1166):9.9< - 我输入的值
唯一的例外似乎是pretty我清楚,无论是 packPrice
或 numPacks
为空或返回的getText
是 getDaily
方法内空。
您需要弄清楚哪些通话的部分实际上是空,并从后向前。
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
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.
这篇关于从MainActivity在Android的碎片的setText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!