它一直显示此错误我一直在寻找此错误的来源。我快疯了。 我尝试了所有替代方案来获取阵列,但没有解决方案。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:229)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:178)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:113)
at com.ab.savr.activity.SetupActivity.SetupActivity2.methodTypes.<init>(methodTypes.java:0)
at com.ab.savr.activity.SetupActivity.SetupActivity2.addMethod.onCreateView(addMethod.java:46)
at android.app.Fragment.performCreateView(Fragment.java:2220)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:482)
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:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以及addmethod
public class addMethod extends DialogFragment {
boolean chooseAType = false;
ArrayList<PaymentMethod> paymentMethods = new ArrayList<>();
Context context;
String[] names;
public addMethod newInstance(Context context) {
this.context = context;
addMethod fragment = new addMethod();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_method_fragment, container);
final Spinner spinner = (Spinner) view.findViewById(R.id.methodSpinner);
names = getResources().getStringArray(R.array.PaymentTypes);
for(int i = 0;i < names.length;i++){
paymentMethods.add(new PaymentMethod(names[i],i));
}
methodTypes methodTypes = new methodTypes(context,paymentMethods);
spinner.setAdapter(methodTypes);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
chooseAType = true;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final EditText editText = (EditText) view.findViewById(R.id.addMethodEditText);
TextView ok = (TextView) view.findViewById(R.id.okButton);
final int pos = spinner.getSelectedItemPosition();
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddMethodFeedback addMethodFeedback = (AddMethodFeedback) getActivity();
if (editText.getText().toString().trim().length() != 0 && !chooseAType) {
getDialog().dismiss();
addMethodFeedback.methodName(editText.getText().toString(), pos);
}
}
});
TextView cancel = (TextView) view.findViewById(R.id.cancelButton);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDialog().dismiss();
}
});
getDialog().setTitle(getTag());
return view;
}
}
以及methodTypes
public class methodTypes extends ArrayAdapter<PaymentMethod> {
Context context;
ArrayList<PaymentMethod> paymentMethods;
int[] icons = {R.drawable.american_express, R.drawable.check, R.drawable.google_wallet, R.drawable.mastercard, R.drawable.mastercard, R.drawable.western_union
, R.drawable.paypal, R.drawable.transfer, R.drawable.visa, R.drawable.visa};
public methodTypes(Context context,ArrayList<PaymentMethod> paymentMethods) {
super(context, 0);
this.context =context;
this.paymentMethods = paymentMethods;
}
@Override
public int getCount() {
return paymentMethods.size();
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
PaymentMethod paymentMethod = paymentMethods.get(position);
convertView = LayoutInflater.from(getContext()).inflate(R.layout.method_item, parent);
TextView name = (TextView) convertView.findViewById(R.id.itemsName);
ImageView imageView = (ImageView) convertView.findViewById(R.id.itemsIcon);
name.setText(paymentMethod.getName());
imageView.setImageResource(icons[paymentMethod.getType()]);
return convertView;
}
public class ViewHolder {
TextView name;
ImageView icon;
}
}
答案 0 :(得分:1)
methodTypes methodTypes = new methodTypes(context,paymentMethods);
上下文在这里可能是空的。
答案 1 :(得分:1)
删除:
Context context;
然后,删除:
public addMethod newInstance(Context context) {
this.context = context;
addMethod fragment = new addMethod();
return fragment;
}
然后,在context
等方法中引用onCreateView()
的地方,将context
替换为getActivity()
。
答案 2 :(得分:1)
您将上下文分配给错误的片段实例,将其替换为:
public addMethod newInstance(Context context) {
addMethod fragment = new addMethod();
fragment.context = context;
return fragment;
}
但我建议您只使用getActivity()
获取Fragment
答案 3 :(得分:1)
你应该从这样的活动中调用片段。
DialogFragment fragment = addMethod.newInstance();//getting fragment.
fragment.show(getActivity().getFragmentManager(), "add_method"); //showing fragment with activity context.
convertView = View.inflate(R.layout.method_item, parent);
使用它。