任何帮助我,我在android studio上都遇到这样的错误:
java.lang.NullPointerException:尝试调用虚拟方法 '布尔com.refresh.pos.domain.sale.kas.addKas1(java.lang.String, double)'在空对象引用上 在com.refresh.pos.ui.sale.AddKasDialogFragment $ 1.onClick(AddKasDialogFragment.java:81) 在android.view.View.performClick(View.java:5721) 在android.widget.TextView.performClick(TextView.java:10936) 在android.view.View $ PerformClick.run(View.java:22620) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:7406) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)06-21 19:25:07.641 22109-22109 / com.refresh.pos I / Process:正在发送信号。 PID:22109 SIG:9
此AddKasDialogFragment.java
public class AddKasDialogFragment extends DialogFragment {
private EditText pengeluaran;
private kas kas;
private EditText ketBox;
private EditText pemasukanBox;
private Button confirmButton;
private Button clearButton;
private UpdatableFragment fragment;
private Resources res;
public AddKasDialogFragment(UpdatableFragment fragment) {
super();
this.fragment = fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layout_addkas, container,
false);
res = getResources();
pengeluaran = (EditText) v.findViewById(R.id.pengeluaranBox);
ketBox = (EditText) v.findViewById(R.id.ketBox);
pemasukanBox = (EditText) v.findViewById(R.id.pemasukanBox);
confirmButton = (Button) v.findViewById(R.id.confirmButton);
clearButton = (Button) v.findViewById(R.id.clearButton);
initUI();
return v;
}
private void initUI() {
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (pengeluaran.getText().toString().equals("")
|| ketBox.getText().toString().equals("")
|| pemasukanBox.getText().toString().equals("")) {
Toast.makeText(getActivity().getBaseContext(),
res.getString(R.string.please_input_all), Toast.LENGTH_SHORT)
.show();
} else {
if (pemasukanBox.getText().toString() == null) {
Toast.makeText(getActivity().getBaseContext(),
res.getString(R.string.please_input_all), Toast.LENGTH_SHORT)
.show();
} else {
String KetBox = ketBox.getText().toString();
boolean success1 = kas.addKas1(KetBox, Double.parseDouble(pemasukanBox.getText()
.toString()));
if (success1) {
Toast.makeText(getActivity().getBaseContext(),
res.getString(R.string.success) + ", "
+ ketBox.getText().toString(),
Toast.LENGTH_SHORT).show();
fragment.update();
clearAllBox();
AddKasDialogFragment.this.dismiss();
} else {
Toast.makeText(getActivity().getBaseContext(),
res.getString(R.string.fail),
Toast.LENGTH_SHORT).show();
}
}
}
}
});
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(ketBox.getText().toString().equals("") && pengeluaran.getText().toString().equals("") && pemasukanBox.getText().toString().equals("")){
AddKasDialogFragment.this.dismiss();
} else {
clearAllBox();
}
}
});
}
private void clearAllBox() {
ketBox.setText("");
pengeluaran.setText("");
pemasukanBox.setText("");
}
}
这个Kas.java
package com.refresh.pos.domain.sale;
import java.util.List;
import com.refresh.pos.techicalservices.kas.KasDao;
public class kas {
private KasDao kasDao;
public kas(KasDao kasDao) {
this.kasDao = kasDao;
}
public boolean addKas1(String keterangan, double jumlah) {
KasDet kasdet = new KasDet(keterangan, jumlah);
int id = kasDao.addKas(kasdet);
return id != -1;
}
}
答案 0 :(得分:1)
错误属于kas
,尚未初始化
else {
String KetBox = ketBox.getText().toString();
kas = new kas(); //change here
boolean success1 = kas.addKas1(KetBox, Double.parseDouble(pemasukanBox.getText()
.toString()));
if (success1) {
Toast.makeText(getActivity().getBaseContext(),
res.getString(R.string.success) + ", "
+ ketBox.getText().toString(),
Toast.LENGTH_SHORT).show();
fragment.update();
clearAllBox();
AddKasDialogFragment.this.dismiss();
}
答案 1 :(得分:0)
阅读您的错误。 kas
为空,因为您从未将其初始化。