我有一些显示平均值的TextView
,然后我有一个类有AlertDialog
方法,我希望用户点击确定并更新我的TextView
值。
我收到错误显示java.lang.NullPointerException:
有人可以教我如何解决它并完成我想要的事情,提前谢谢。
TextView
布局类扩展了Fragment:
public class MyDailyInput extends Fragment implements View.OnClickListener{
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_daily_input, container, false);
//contact is my data object , it has data i'm sure.
new DialogHandler(getActivity()).
updateDialog(getResources().getString(R.string.update),
getResources().getString(R.string.cancel), contact);
return view;
}
// Set the average value method
public void setAverageValue(Context context){
avBeMorning.setText(String.valueOf(av.beforeMorning()));
avAfMorning.setText(String.valueOf(av.afterMorning()));
avBeNoon.setText(String.valueOf(av.beforeNoon()));
avAfNoon.setText(String.valueOf(av.afterNoon()));
avBeNight.setText(String.valueOf(av.beforeNight()));
avAfNight.setText(String.valueOf(av.afterNight()));
}
这是我的类控制AlertDialog:
public class DialogHandler extends AppCompatActivity {
private DatabaseHandler db =null;
private Context context;
public DialogHandler(Context context){
this.context = context;
db = new DatabaseHandler(context);
}
// AlertDialog
public void updateDialog(String rightMessage, String leftMessage, final Contact contact) {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
builder.setMessage(context.getResources().getString(R.string.dataExisted))
.setPositiveButton(rightMessage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.out.println("try to update");
Toast.makeText(context,context.getResources().getString(R.string.updatedSucceed),Toast.LENGTH_SHORT).show();
db.updateContact(contact);
// I try to use this code to
// change my TextView value of
// MyDailyInut layout , but i get error.
new MyDailyInput().setAverageValue(context);
}
});
builder.setNegativeButton(leftMessage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}
}
答案 0 :(得分:0)
你必须在fragment中创建一个接口,activity应该实现该接口的方法并在fragment的onCreate方法上初始化接口。
//片段
public class FragmentOne extends Fragment {
private OnClicked call;
public interface OnClicked {
void gotoActivity(int value);
}
@Override
public void onAttach(Context context){
super.onAttach(context);
call = (OnClicked) context;
}
//any event
call.gotoActivity(your average value);
}
public class RandomActivity extends Activity implements FragmentOne.OnClicked {
@Override
public void gotoActivity(int value) {
// do here
}
}
答案 1 :(得分:0)
在片段本身内创建alertDialog而不是创建新活动
使用getActivity()
代替context
public class MyDailyInput extends Fragment implements View.OnClickListener{
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my_daily_input, container, false);
updateDialog(getResources().getString(R.string.update),
getResources().getString(R.string.cancel), contact);
return view;
}
// Set the average value method
public void setAverageValue(Context context){
avBeMorning.setText(String.valueOf(av.beforeMorning()));
avAfMorning.setText(String.valueOf(av.afterMorning()));
avBeNoon.setText(String.valueOf(av.beforeNoon()));
avAfNoon.setText(String.valueOf(av.afterNoon()));
avBeNight.setText(String.valueOf(av.beforeNight()));
avAfNight.setText(String.valueOf(av.afterNight()));
}
public void updateDialog(String rightMessage, String leftMessage, final Contact contact) {
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
builder.setMessage(getActivity().getResources().getString(R.string.dataExisted))
.setPositiveButton(rightMessage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
System.out.println("try to update");
Toast.makeText(getActivity(),getActivity().getResources().getString(R.string.updatedSucceed),Toast.LENGTH_SHORT).show();
db.updateContact(contact);
// I try to use this code to
// change my TextView value of
// MyDailyInut layout , but i get error.
setAverageValue(getActivity());
}
});
builder.setNegativeButton(leftMessage, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
}