我有一个带有复选框和重置按钮的android片段。 片段内部有更新和重置方法。 每当我调用这些方法时,我都会得到一个NullPointerException。 我无法弄清楚为什么(这让我成为新手)
package info.doktershuis.android.jichtcalculator;
public class CalculatorFragment extends Fragment implements View.OnClickListener {
double score = 0;
double man = 2;
double manPl = 0;
public CalculatorFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_calculator, container, false);
CheckBox cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht);
cbGeslacht.setOnClickListener(this);
Button reset = (Button) layout.findViewById(R.id.button);
reset.setOnClickListener(this);
TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
TextView advies = (TextView) layout.findViewById(R.id.tvToelicht);
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
return layout;
}
public void reset(View view) {
CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht);
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
geslacht.setChecked(false);
double score = 0;
manPl = 0;
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
}
public void update(View view) {
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
score = manPl;
punt.setText(Double.toString(score));
if (score <= 4) {
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
} else if (score < 8) {
perc.setText(R.string.pt48);
perc.setBackgroundColor(getResources().getColor(holo_orange_light));
advies.setText(R.string.pt48a);
} else {
perc.setText(R.string.pt8);
perc.setBackgroundColor(getResources().getColor(holo_red_light));
advies.setText(R.string.pt8a);
}
}
@Override
public void onClick(View view) {
CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht);
switch (view.getId()) {
case R.id.cbGeslacht:
if (geslacht.isChecked()) {
manPl = man;
} else {
manPl = 0;
}
update(view);
break;
case R.id.button:
reset(view);
break;
}
}
}
当我使用geslacht.setChecked(false);
或punt.setText(Double.toString(score));
时,会抛出异常
任何人都可以帮助我这个,非常感谢提前
logcat的:
01-21 08:43:25.233 23876-23876/info.doktershuis.android.jichtcalculator E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at info.doktershuis.android.jichtcalculator.CalculatorFragment.reset(CalculatorFragment.java:111)
at info.doktershuis.android.jichtcalculator.CalculatorFragment.onClick(CalculatorFragment.java:278)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
嗨K Neeraj Lal(SO-not-ready to help),我已经阅读了有关NullPointerException的所有内容。找不到我的答案。请指出我在哪里找到它。记住我是新手。我可能不会理解这一切。
答案 0 :(得分:0)
由于我没有看到您的错误日志,但仍有少数观察可能导致Null指针异常。
您每次使用按钮视图在按钮单击方法中投射视图。喜欢这里
public void update(View view) {
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
在这里
public void reset(View view) {
CheckBox geslacht = (CheckBox) view.findViewById(R.id.cbGeslacht);
TextView punt = (TextView) view.findViewById(R.id.tvPunt);
TextView perc = (TextView) view.findViewById(R.id.tvPercentage);
TextView advies = (TextView) view.findViewById(R.id.tvToelicht);
最好将这些视图设为全局视图,并从update
和reset
方法中删除这些视图,因为您已经在onCreateView
方法中添加了这些视图。 lemme知道问题是否解决,或者发布错误日志。 :)
这里是样本
public class CalculatorFragment extends Fragment implements View.OnClickListener {
double score = 0;
double man = 2;
double manPl = 0;
CheckBox cbGeslacht;
TextView punt;
TextView perc;
TextView advies;
public CalculatorFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_calculator, container, false);
cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht);
cbGeslacht.setOnClickListener(this);
Button reset = (Button) layout.findViewById(R.id.button);
reset.setOnClickListener(this);
punt = (TextView) layout.findViewById(R.id.tvPunt);
perc = (TextView) layout.findViewById(R.id.tvPercentage);
advies = (TextView) layout.findViewById(R.id.tvToelicht);
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
return layout;
}
public void reset(View view) {
cbGeslacht.setChecked(false);
double score = 0;
manPl = 0;
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
}
public void update(View view) {
score = manPl;
punt.setText(Double.toString(score));
if (score <= 4) {
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
} else if (score < 8) {
perc.setText(R.string.pt48);
perc.setBackgroundColor(getResources().getColor(holo_orange_light));
advies.setText(R.string.pt48a);
} else {
perc.setText(R.string.pt8);
perc.setBackgroundColor(getResources().getColor(holo_red_light));
advies.setText(R.string.pt8a);
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.cbGeslacht:
if (cbGeslacht.isChecked()) {
manPl = man;
} else {
manPl = 0;
}
update(view);
break;
case R.id.button:
reset(view);
break;
}
}
}
答案 1 :(得分:0)
您可以使用布局而不是获取按钮视图来获取其他项目。
试试这个
public class CalculatorFragment extends Fragment implements View.OnClickListener {
double score = 0;
double man = 2;
double manPl = 0;
View layout;
public CalculatorFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layout = inflater.inflate(R.layout.fragment_calculator, container, false);
CheckBox cbGeslacht = (CheckBox)layout.findViewById(R.id.cbGeslacht);
cbGeslacht.setOnClickListener(this);
Button reset = (Button) layout.findViewById(R.id.button);
reset.setOnClickListener(this);
TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
TextView advies = (TextView) layout.findViewById(R.id.tvToelicht);
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
return layout;
}
public void reset(View view) {
CheckBox geslacht = (CheckBox) layout.findViewById(R.id.cbGeslacht);
TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
TextView advies = (TextView) layout.findViewById(R.id.tvToelicht);
geslacht.setChecked(false);
double score = 0;
manPl = 0;
punt.setText(Double.toString(score));
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
}
public void update(View view) {
TextView punt = (TextView) layout.findViewById(R.id.tvPunt);
TextView perc = (TextView) layout.findViewById(R.id.tvPercentage);
TextView advies = (TextView) layout.findViewById(R.id.tvToelicht);
score = manPl;
punt.setText(Double.toString(score));
if (score <= 4) {
perc.setText(R.string.pt4);
perc.setBackgroundColor(getResources().getColor(holo_green_light));
advies.setText(R.string.pt4a);
} else if (score < 8) {
perc.setText(R.string.pt48);
perc.setBackgroundColor(getResources().getColor(holo_orange_light));
advies.setText(R.string.pt48a);
} else {
perc.setText(R.string.pt8);
perc.setBackgroundColor(getResources().getColor(holo_red_light));
advies.setText(R.string.pt8a);
}
}
@Override
public void onClick(View view) {
CheckBox geslacht = (CheckBox) layout.findViewById(R.id.cbGeslacht);
switch (view.getId()) {
case R.id.cbGeslacht:
if (geslacht.isChecked()) {
manPl = man;
} else {
manPl = 0;
}
update(view);
break;
case R.id.button:
reset(view);
break;
}
}
}