inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
代码:
public class QuestionOptionsAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final Context context;
private final boolean viewQuestion;
List<QuestionOption> optionList;
private String questionId;
String type = "";
private Dialog dialog = null;
private boolean answer = false;
private boolean isFinshed;
private Question mQuestion;
private static final int NOT_SELECTED = -1;
private int selectedPos = NOT_SELECTED;
boolean isClicked = false;
protected DialogManager alertDialog;
protected ExamGuruApplication.Listener mListener;
private int pos ;
private boolean isShowError ;
public QuestionOptionsAdapter(Context context, List<QuestionOption> optionList, String questionId, boolean viewQuestion, String type, boolean answer, boolean isFinshed, Question mQuestion, ExamGuruApplication.Listener mListener, int position, boolean isShowError) {
this.context = context;
// this.mFragment = mFragment;
this.isFinshed = isFinshed;
this.viewQuestion = viewQuestion;
this.optionList = null;
this.optionList = optionList;
this.questionId = questionId;
this.mQuestion = mQuestion;
this.mListener = mListener;
this.pos = position;
this.type = type;
this.isShowError = isShowError;
this.answer = answer;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
}
@Override
public int getCount() {
return optionList.size();
}
@Override
public Object getItem(int position) {
return optionList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
final ViewHolder holder;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(R.layout.option_list_item, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
alertDialog = DialogManager.getInstance();
QuestionOption option = optionList.get(position);
holder.option_correct_incorrect_image.setVisibility(View.INVISIBLE);
holder.option_radio_btn.setChecked(false);
holder.optionText.setText(option.getEntry() + ".");
holder.option_radio_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedPos != position) {
if (!isFinshed) {
isClicked = true;
answerMarking(position);
setSelection(position);
mListener.isClicked();
}
}
}
});
boolean isCame = false;
if(ExamGuruApplication.getTutorList().get(pos) != null && !ExamGuruApplication.getTutorList().get(pos)) {
if ((!mQuestion.getLocked() && type.equals("tutor")) && !isFinshed && option.getEntry() != null && mQuestion.getSelectedOption() != null && option.getId().equals(mQuestion.getSelectedOption().getId()) && ExamGuruApplication.getListHeight().get(pos) == null) {
ExamGuruApplication.getListHeight().put(pos, position);
mQuestion.getSelectedOption().setId(0);
}
if ((!mQuestion.getLocked() && type.equals("tutor")) && !isFinshed && ExamGuruApplication.getListHeight().containsKey(pos) && ExamGuruApplication.getListHeight().get(pos) != null) {
if (ExamGuruApplication.getListHeight().get(pos) == position) {
holder.option_radio_btn.setChecked(true);
System.out.println("one");
notifyDataSetChanged();
isCame = true;
} else {
holder.option_radio_btn.setChecked(false);
isCame = false;
}
}
}else if(ExamGuruApplication.getTutorList().get(pos) != null && ExamGuruApplication.getTutorList().get(pos)){
if (ExamGuruApplication.getListHeight() != null && ExamGuruApplication.getListHeight().size() >0 && ExamGuruApplication.getListHeight().get(pos) != null && ExamGuruApplication.getListHeight().get(pos) == position) {
holder.option_radio_btn.setChecked(true);
System.out.println("two");
if(ExamGuruApplication.getTutorList().get(pos) != null && ExamGuruApplication.getTutorList().get(pos)) {
holder.option_correct_incorrect_image.setBackground(context.getResources().getDrawable(R.drawable.wrong));
holder.option_correct_incorrect_image.setVisibility(View.VISIBLE);
}
notifyDataSetChanged();
}
}else{
if (!isFinshed && option.getEntry() != null && mQuestion.getSelectedOption() != null && option.getId().equals(mQuestion.getSelectedOption().getId()) && ExamGuruApplication.getListHeight().get(pos) == null) {
ExamGuruApplication.getListHeight().put(pos, position);
mQuestion.getSelectedOption().setId(0);
}
if (!isFinshed && ExamGuruApplication.getListHeight().containsKey(pos) && ExamGuruApplication.getListHeight().get(pos) != null) {
if (ExamGuruApplication.getListHeight().get(pos) == position) {
holder.option_radio_btn.setChecked(true);
// System.out.println("showError "+ExamGuruApplication.getShowErrorList().get(position) != null ? ExamGuruApplication.getShowErrorList().get(position): false);
if(ExamGuruApplication.getShowErrorList().get(position) != null && ExamGuruApplication.getShowErrorList().get(position) && mQuestion.getLocked()){
holder.option_correct_incorrect_image.setBackground(context.getResources().getDrawable(R.drawable.wrong));
holder.option_correct_incorrect_image.setVisibility(View.VISIBLE);
}
System.out.println("three");
notifyDataSetChanged();
isCame = true;
} else {
holder.option_radio_btn.setChecked(false);
isCame = false;
}
}
}
holder.optionDescription.setText(option.getDetails());
holder.optionDescription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedPos != position) {
if (!isFinshed) {
isClicked = true;
answerMarking(position);
setSelection(position);
mListener.isClicked();
}
}
}
});
if (isFinshed) {
holder.option_radio_btn.setClickable(false);
}
if (mQuestion.getIsLocked()) {
holder.optionDescription.setClickable(false);
holder.optionDescription.setEnabled(false);
holder.option_radio_btn.setClickable(false);
holder.option_radio_btn.setEnabled(true);
}
if (option != null && option.getCorrect() != null && answer && option.getCorrect()) {
if (isFinshed && !isClicked || mQuestion.getIsLocked()) {
setSelection(position);
holder.option_correct_incorrect_image.setBackground(context.getResources().getDrawable(R.drawable.right));
holder.option_correct_incorrect_image.setVisibility(View.VISIBLE);
if (!isFinshed) {
if (!viewQuestion) {
// answerMarking(position);
}
}
if (mQuestion.getSelectedOption() != null) {
if (option.getId().equals(mQuestion.getSelectedOption().getId())) {
System.out.println("four");
holder.option_radio_btn.setChecked(true);
}else{
holder.option_radio_btn.setChecked(false);
}
}
holder.optionDescription.setClickable(false);
holder.optionDescription.setEnabled(false);
holder.option_radio_btn.setClickable(false);
holder.option_radio_btn.setEnabled(true);
}
}
if (mQuestion != null && !isCame) {
if (!isFinshed && mQuestion.getSelectedOption() != null && !mQuestion.getIsLocked()) {
if(position ==0 && option.getId().equals(mQuestion.getSelectedOption().getId())){
holder.option_radio_btn.setChecked(false);
}
if (option.getEntry() != null && option.getId().equals(mQuestion.getSelectedOption().getId())) {
if (!isFinshed && !isClicked) {
if (!viewQuestion) {
// answerMarking(position);
// setSelection(position);
if (!isClicked) {
holder.optionDescription.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
holder.optionText.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
System.out.println("=========" + position + "=========" + selectedPos);
System.out.println("five");
// holder.option_radio_btn.setChecked(true);
isClicked = false;
}
}
}
}
} else if (isFinshed && mQuestion.getSelectedOption() != null || mQuestion.getIsLocked()) {
if (mQuestion.getSelectedOption() != null && option.getId().equals(mQuestion.getSelectedOption().getId())) {
if (option.getCorrect() != null && !option.getCorrect()) {
holder.option_correct_incorrect_image.setBackground(context.getResources().getDrawable(R.drawable.wrong));
holder.option_correct_incorrect_image.setVisibility(View.VISIBLE);
holder.option_radio_btn.setChecked(true);
System.out.println("six");
holder.option_radio_btn.setClickable(false);
}
}
}
}
if (position == selectedPos && isClicked) {
holder.optionDescription.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
holder.optionText.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
System.out.println("=========" + position + "=========" + selectedPos);
holder.option_radio_btn.setChecked(true);
System.out.println("seven");
} else if (isClicked) {
holder.optionDescription.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
holder.optionText.setTextColor(ContextCompat.getColor(context, R.color.BLACK));
System.out.println("========="+position+"========="+selectedPos);
holder.option_radio_btn.setChecked(false);
}
final View ref = convertView;
final ViewHolder holderRef = holder;
// ExamGuruApplication.setHeight(ExamGuruApplication.getHeight()+holder.optionDescription.getHeight() );
return convertView;
}
// if called with the same position multiple lines it works as toggle
public void setSelection(int position) {
if (selectedPos == position) {
selectedPos = NOT_SELECTED;
} else {
selectedPos = position;
}
}
static class ViewHolder {
private TextView optionText;
private TextView optionDescription;
private TextView option_correct_incorrect_image;
private RadioButton option_radio_btn;
int distanceFromTop;
public ViewHolder(View view) {
optionText = (TextView) view.findViewById(R.id.option_text);
optionDescription = (TextView) view.findViewById(R.id.option_description);
option_correct_incorrect_image = (TextView) view.findViewById(R.id.option_correct_incorrect_image);
option_radio_btn = (RadioButton) view.findViewById(R.id.option_radio_btn);
}
}
/**
* Creates and displays a progress dialog if one does not already exist
*/
protected void showProgressDialog() {
try {
if (dialog == null) {
dialog = new Dialog(context, R.style.CustomProgressTheme);
dialog.setContentView(R.layout.custom_progress);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Dismiss Progress Dialog
*/
protected void dismissProgressDialog() {
if (dialog != null && dialog.isShowing()) {
try {
dialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
dialog = null;
}
private void answerMarking(final int position) {
MarkedAnswers mMarkedAnswers = new MarkedAnswers();
mMarkedAnswers.setId(optionList.get(position).getId().toString());
mMarkedAnswers.setIsGuessed(false);
if (type.equalsIgnoreCase("timed")) {
long endTime = System.currentTimeMillis();
long seconds = (endTime - ExamGuruApplication.getStartTime()) / 1000;
mMarkedAnswers.setAnsweringTime((int) seconds);
}
// showProgressDialog();
ExamGuruManger.getInstance().markedQuestionAnswer(mMarkedAnswers, String.valueOf(UserPreferences.getId()), questionId, new Callback<Response>() {
@Override
public void success(Response response, Response response2) {
// dismissProgressDialog();
ExamGuruApplication.setStartTime(System.currentTimeMillis());
SelectedOption mSelectedOption = new SelectedOption();
mSelectedOption.setId(optionList.get(position).getId());
mSelectedOption.setDetails(optionList.get(position).getDetails());
mSelectedOption.setEntry(optionList.get(position).getEntry());
mQuestion.setSelectedOption(mSelectedOption);
ExamGuruApplication.getListHeight().put(pos, position);
notifyDataSetChanged();
}
@Override
public void failure(RetrofitError error) {
// dismissProgressDialog();
alertDialog.showDialog(context, "", error.getLocalizedMessage(), context.getString(R.string.alert_ok), "");
}
});
}
public void notifyChanged() {
this.notifyDataSetChanged();
}
}
上述inflater服务仅在Android 6.0.1中创建问题,仅限samsung S6设备。其他设备按预期工作。
例外:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at com.comquest.adapter.QuestionOptionsAdapter.(SourceFile:68)
at com.comquest.fragment.ViewPagerFragment$5.success(SourceFile:654)
at com.comquest.fragment.ViewPagerFragment$5.success(SourceFile:648)
at retrofit.CallbackRunnable$1.run(SourceFile:45)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
注意:使用来自用户端的崩溃分析检测到此日志。
答案 0 :(得分:0)
根据您的日志,如果您的上下文为空,则非常简单地检查您是否只会获得null inflater。因此,在初始化inflater之前尝试检查上下文
import matplotlib.pyplot as plt
from matplotlib import gridspec
import gizmo
import utilities as ut
part=gizmo.io.Read.read_snapshots('all', 'redshift', 0, element_indices=None)
plt.figure()
plt.subplot(2,1,1)
gizmo.analysis.Image.plot_image(part, 'dark', 'mass', 'histogram', [0,1],[0,1,2], 300, 0.5, use_column_units=None)
plt.subplot(2,2,1)
gizmo.analysis.Image.plot_image(part, 'dark', 'mass', 'histogram', [0,2],[0,1,2], 300, 0.5, use_column_units=None)
plt.subplot(2,2,2)
gizmo.analysis.Image.plot_image(part, 'dark', 'mass', 'histogram', [1,2],[0,1,2], 300, 0.5, use_column_units=None)
plt.tight_layout()
plt.savefig('myfigure.png', dpi=900)
plt.show()
答案 1 :(得分:0)
尝试
inflater = LayoutInflater.from(context);
在构造&#39; QuestionOptionsAdapter&#39;时使用ApplicationContext insted of activity context。