gpt4 book ai didi

java - 当我尝试比较数据库中的值和编辑文本输入时,出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 20:27:25 26 4
gpt4 key购买 nike

因此,我尝试比较 2 个值,一个是 Firebase 中的“coinAmount”,它是一个 Long 值,另一个是 EditText 输入(这是一个字符串)。

基本上我想要实现的是,如果输入超过用户拥有的硬币数量,我想显示警告。

        String coinAmount = coinWrapper.getEditText().getText().toString();

if (Long.parseLong(coinAmount) >= coinValue) {

Toast.makeText(getApplicationContext(), "You don't have enough coins!",
Toast.LENGTH_SHORT).show();
} else {

Intent intent = new Intent(getActivity(), FriendActivity.class);
intent.putExtra("coin", coinAmount);
intent.putExtra("choice", choice);
startActivity(intent);
}

我将用户输入作为字符串的原因是因为我稍后将将该值显示为文本,所以在比较时我将其解析为长整型。但是,当我运行该应用程序时,我收到此 NullPointerException:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: ege.mevzubahis, PID: 13369
java.lang.NullPointerException
at ege.mevzubahis.Fragments.BetsDialogFragment.onClick(BetsDialogFragment.java:135)
at ege.mevzubahis.Fragments.BetsDialogFragment_ViewBinding$4.doClick(BetsDialogFragment_ViewBinding.java:70)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:4478)
at android.view.View$PerformClick.run(View.java:18698)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)

这是我尝试从 Firebase 获取 coinAmount 的部分:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
userID=sharedPreferences.getString("userIDKey",null);


mDatabase.child("Users").child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

coinValue = (Long) dataSnapshot.child("coin").getValue();

}

@Override
public void onCancelled(DatabaseError databaseError) {

//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

}
});

这部分有错误:

if (Long.parseLong(coinAmount) >= coinValue) {
<小时/>

完整代码

public class BetsDialogFragment extends DialogFragment implements View.OnClickListener {

@BindView(R.id.tv_match) TextView tv_match;
@BindView(R.id.textView2) TextView textView2;
@BindView(R.id.textInputLayout) TextInputLayout textInputLayout;
@BindView(R.id.radioButton4) RadioButton radioButton4;
@BindView(R.id.radioButton5) RadioButton radioButton5;
@BindView(R.id.radioButton6) RadioButton radioButton6;
@BindView(R.id.button2) Button sendButton;

String matchName;
private DatabaseReference mDatabase;
private RadioGroup radioGroup;
private String choice;
private EditText editText;
private TextInputLayout coinWrapper;
public Long coinValue;

SharedPreferences sharedPreferences;
private String userID;

@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom_dialog, null);
matchName = getArguments().getString("betNameInPosition");
mDatabase = FirebaseDatabase.getInstance().getReference();
radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
editText = (EditText) view.findViewById(R.id.editText);
coinWrapper = (TextInputLayout) view.findViewById(R.id.textInputLayout);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
userID=sharedPreferences.getString("userIDKey",null);


mDatabase.child("Users").child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

coinValue = (Long) dataSnapshot.child("coin").getValue();

Log.v("coin value: ", coinValue.toString());

}

@Override
public void onCancelled(DatabaseError databaseError) {

//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

}
});

mDatabase.child("Bets")
.child("Sports")
.child(matchName)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override public void onDataChange(DataSnapshot dataSnapshot) {
try {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
String value = (String) map.get("matchname").toString();
tv_match.setText(value);
String durationValue = (String) map.get("duration").toString();
textView2.setText("Due to: " + durationValue);

coinValue = (Long) dataSnapshot.child("coin").getValue();
} catch (Throwable t) {
Log.e("trycatchFAIL", "b");
}
}

@Override public void onCancelled(DatabaseError databaseError) {

}
});

ButterKnife.bind(this, view);
return view;
}

@OnClick({ R.id.radioButton4, R.id.radioButton5, R.id.radioButton6})
public void onClick(View view) {

switch (view.getId()) {
case R.id.radioButton4:
choice = "home";
Toast.makeText(getApplicationContext(), "choice: Home", Toast.LENGTH_SHORT).show();
break;
case R.id.radioButton5:
choice = "draw";
Toast.makeText(getApplicationContext(), "choice: Draw", Toast.LENGTH_SHORT).show();
break;
case R.id.radioButton6:
choice = "away";
Toast.makeText(getApplicationContext(), "choice: Away", Toast.LENGTH_SHORT).show();
break;

}
}

@OnClick(R.id.button2)
public void onClickSendButton(View view){
String coinAmount = coinWrapper.getEditText().getText().toString();

Log.v("coinAmount",coinAmount);

if (Long.parseLong(coinAmount) >= coinValue) {

Toast.makeText(getApplicationContext(), "You don't have enough coins!",
Toast.LENGTH_SHORT).show();
} else {

Intent intent = new Intent(getActivity(), FriendActivity.class);
intent.putExtra("coin", coinAmount);
intent.putExtra("choice", choice);
startActivity(intent);
}


}
}

最佳答案

if (Long.parseLong(coinAmount) >= coinValue).  

有问题:
如果 coinAmount 为 null,将抛出 java.lang.NumberFormatException:null所以看起来 coinValue 为空。
因为使用了“>=”,Java会尝试改变Long -> long。如果该项为 null,jvm 将抛出 java.lang.NullPointerException

关于java - 当我尝试比较数据库中的值和编辑文本输入时,出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43662723/

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