gpt4 book ai didi

android - 为什么我不能因为注释而在 onCreate() 中添加方法?

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:12 24 4
gpt4 key购买 nike

我是 Android 新手,使用这些方法设置默认值时遇到问题

 @SuppressLint("StringFormatMatches")
public void setTipValues() {

tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult,finalBillAmount));
tvTipAmount.setText(getString(R.string.main_msg_tipTotal,tipTotal));
}

并计算最终账单

  private void CalculteFinalBill() {
if(Percentage==0)
Percentage=Default_Tip_Percent;
if(!etBillAmount.getText().toString().equals("") && !etBillAmount.getText().toString().equals("."))

totalBillAmount=Float.valueOf(etBillAmount.getText().toString());
else
totalBillAmount=0;
tipTotal=(totalBillAmount*Percentage)/100;
finalBillAmount=totalBillAmount+tipTotal ;
}

然后我将这些字符串添加到 strings.xml

<resources>
<string name="app_name">Tip Calculator</string>
<string name="main_msg_billAmount">Bill AMOUNT</string>
<string name="main_hint_billAmount">Enter Bill Amount Here</string>
<string name="main_msg_serviceRating">Service Rating</string>
<string name="main_msg_tipPercent">%s%% Tip Percent</string>
<string name="main_msg_tipTotal">$%s Tip Total</string>
<string name="main_msg_billTotal">Bill Total</string>
<string name="main_msg_billTotalResult">$%s</string>

变量是

 TextView tvTipPercent;
TextView tvBillTotalAmount;
TextView tvTipAmount;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;

当我尝试添加 setTipValues();到Oncreate 并运行我的应用程序我有一个致命的异常

03-23 08:56:00.768 1836-1836/com.example.tipcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tipcalculator/com.example.tipcalculator.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tipcalculator.MainActivity.setTipValues(MainActivity.java:48)
at com.example.tipcalculator.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

在第 48 行中引用了 `

 tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));

我知道 butterKnife 解决了这个注释问题,但我不使用它怎么办

谁能帮帮我?

    public class MainActivity extends AppCompatActivity {
TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;
float Percentage = 0;
float tipTotal = 0;
float finalBillAmount = 0;
float Regular_Tip_Percent = 10;
float Default_Tip_Percent = 15;
float Excellent_Tip_Percent = 20;
float totalBillAmount;
ImageButton Im1;
ImageButton Im2;
ImageButton Im3;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etBillAmount=(EditText)findViewById(R.id.etBillAmount);
final TextView tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
final TextView tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
final TextView tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);


setTipValues();

}


@SuppressLint("StringFormatMatches")
public void setTipValues() {

tvTipPercent.setText(getString(R.string.main_msg_tipPercent, Percentage));
tvBillTotalAmount.setText(getString(R.string.main_msg_billTotalResult, finalBillAmount));
tvTipAmount.setText(getString(R.string.main_msg_tipTotal, tipTotal));
}
}

最佳答案

Welcome to SO, first off you should read how toask; remember to never addimages of code, but add the entire code to the answer, because it isreally hard to replicate any problem having an image of it instead ofthe text (which can be copy-pasted).

现在,哪个是你的行抛出错误? MainActivity:java:48 指的是哪一行?

此错误意味着您正在尝试访问一个为空的变量

请添加您代码中抛出错误的第 48 行;看看What is NullPointerException

PS:这不完全是一个答案,但为了帮助您,我更愿意在此处添加一些提示,而不是发布未格式化的评论。一旦您提供了所需的信息,我将对此进行改进


根据您的新信息进行编辑

我认为你的问题在于你如何初始化变量:在 OnCreate 方法中你调用这样的东西:

    final EditText etBillAmount=(EditText)findViewById(R.id.etBillAmount);
final TextView tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
final TextView tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
final TextView tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);

首先,您不需要 final 属性;但更重要的是你正在创建 4 个新的局部变量,你没有使用你的 class-scoped 那些:

TextView tvTipAmount;
TextView tvBillTotalAmount;
TextView tvTipPercent;
EditText etBillAmount;

如果你想使用你的类变量,你只需要调用它们而不需要重新创建新的,就像这样:

    etBillAmount=(EditText)findViewById(R.id.etBillAmount);
tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
tvBillTotalAmount = (TextView) findViewById(R.id.tvBillTotalAmount);
tvTipPercent = (TextView) findViewById(R.id.tvTipPercent);

如您所见,由于您已经在 Activity 中创建了它们,因此您只需调用它们即可获得访问权限。

就是说,您的错误出现是因为您没有初始化 class-variables,而是创建了新的。因此,下次您尝试访问它们时,它们将是 null

关于android - 为什么我不能因为注释而在 onCreate() 中添加方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55304228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com