片段:尽管使用 onAttach(),但方向改变后 onTextChanged null 内的回调
Fragment: Callback inside onTextChanged null after orientation change despite using onAttach()
我的片段总是抛出
java.lang.NullPointerException: Attempt to invoke interface method 'void FragmentClass$InputCallbacks.OnTextChanged(java.lang.CharSequence)' on a null object reference
因为行:
UserInput.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mListener.OnTextChanged(s);
}
});
我对 mListener 的其他用途不是抛出任何东西。
我想这是因为使用了 new Textwatcher(){....} 但我没有足够的经验自己解决这个问题:-/
整个class抛出错误:
public class ToolbarInputOneAddress extends Fragment {
private InputCallbacks mListener;
public ToolbarInputOneAddress() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.toolbar_input_one_address, container, false);
Address address = mListener.getAddress();
EditText UserInput = (EditText) v.findViewById(R.id.InputField);
if (address != null) {
StringBuilder Sb = new StringBuilder();
UserInput.setText(SuggestAddresses.createFormattedAddressFromAddress(address, Sb));
}
UserInput.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mListener.OnTextChanged(s);
}
});
ImageButton ReturnButton = (ImageButton) v.findViewById(R.id.btn_navigation_drawer_back);
ReturnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onReturnButtonClicked();
}
});
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (InputCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement InputCallbacks");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface InputCallbacks {
void OnTextChanged(CharSequence s);
void onReturnButtonClicked();
Address getAddress();
}
}
我想我解决了。
我在像这样的组中的片段中声明我的回调
public interface $Callbacks {
void $Callback1(String s);
void $Callback2();
Address $Callback2();
}
原来我定义了多个同名的回调组。我认为这没问题,因为我在 activity.
中用 implements
明确说明了这一点
自从我将回调组名称更改为不同的名称后,我不再获得 NPE:
长话短说:
我变了
implements $fragment1.callbacks $fragment2.callbacks $fragment3.callbacks
到
implements $fragment1.f1callbacks $fragment2.f2callbacks $fragment3.f3callbacks
在我的 activity 实现所述片段回调中
我的片段总是抛出
java.lang.NullPointerException: Attempt to invoke interface method 'void FragmentClass$InputCallbacks.OnTextChanged(java.lang.CharSequence)' on a null object reference
因为行:
UserInput.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mListener.OnTextChanged(s);
}
});
我对 mListener 的其他用途不是抛出任何东西。 我想这是因为使用了 new Textwatcher(){....} 但我没有足够的经验自己解决这个问题:-/
整个class抛出错误:
public class ToolbarInputOneAddress extends Fragment {
private InputCallbacks mListener;
public ToolbarInputOneAddress() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.toolbar_input_one_address, container, false);
Address address = mListener.getAddress();
EditText UserInput = (EditText) v.findViewById(R.id.InputField);
if (address != null) {
StringBuilder Sb = new StringBuilder();
UserInput.setText(SuggestAddresses.createFormattedAddressFromAddress(address, Sb));
}
UserInput.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mListener.OnTextChanged(s);
}
});
ImageButton ReturnButton = (ImageButton) v.findViewById(R.id.btn_navigation_drawer_back);
ReturnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onReturnButtonClicked();
}
});
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (InputCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement InputCallbacks");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface InputCallbacks {
void OnTextChanged(CharSequence s);
void onReturnButtonClicked();
Address getAddress();
}
}
我想我解决了。 我在像这样的组中的片段中声明我的回调
public interface $Callbacks {
void $Callback1(String s);
void $Callback2();
Address $Callback2();
}
原来我定义了多个同名的回调组。我认为这没问题,因为我在 activity.
中用implements
明确说明了这一点
自从我将回调组名称更改为不同的名称后,我不再获得 NPE:
长话短说:
我变了
implements $fragment1.callbacks $fragment2.callbacks $fragment3.callbacks
到
implements $fragment1.f1callbacks $fragment2.f2callbacks $fragment3.f3callbacks
在我的 activity 实现所述片段回调中