My Fragment always throws
java.lang.NullPointerException: Attempt to invoke interface method 'void FragmentClass$InputCallbacks.OnTextChanged(java.lang.CharSequence)' on a null object reference
because of the line:
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);
}
});
My other uses of mListener aren't throwing something.
I guess it's because of the use of new Textwatcher(){....} but I'm not experienced enough to solve that by myself :-/
The whole class throwing the error:
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();
}
}
I think I solved it.
I declare my callbacks in fragments in groups like this like this
public interface $Callbacks {
void $Callback1(String s);
void $Callback2();
Address $Callback2();
}
Turns out I had multiple Callback Groups defined with the same name. I thought this is okay, because I state that explicitly with implements in my activity.
Since I changed the callback groups names to different ones I don't get the NPE anymore:
TL,DR:
I changed
implements $fragment1.callbacks $fragment2.callbacks $fragment3.callbacks
to
implements $fragment1.f1callbacks $fragment2.f2callbacks $fragment3.f3callbacks
In my activity implementing said fragment callbacks
Related
I have an SDK that contains MYSDKActivity. In this activity layout, I have 1 edit text where the user can enter his card number. Any android Client who is integrating this SDK, can use ActivityLifecycleCallbacks in their application class and register text watcher on this edit text and can listen to user card number input.
I want to prevent SDK's edit text usage via the Application class. Only my SDK can access this edit text and perform some action. I want to block usage outside my SDK.
public class MyDemoApp extends Application implements Application.ActivityLifecycleCallbacks {
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
#Override
public void onActivityCreated(#NonNull Activity activity, #Nullable Bundle savedInstanceState) {}
#Override
public void onActivityStarted(#NonNull Activity activity) {}
#Override
public void onActivityResumed(#NonNull Activity activity) {
if (activity instanceof MYSDKActivity) {
final TextInputLayout textInputLayout = activity.findViewById(R.id.my_sdk_edit_text);
if (textInputLayout != null) {
ViewGroup textInputLayoutViewGroup = ((ViewGroup) textInputLayout.getChildAt(0));
for (int i = 0; i < textInputLayoutViewGroup.getChildCount(); i++) {
View child = textInputLayoutViewGroup.getChildAt(i);
if (child instanceof TextInputEditText) {
Log.v("USER Input", ((TextInputEditText) child).getText().toString());
((TextInputEditText) child).addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v("USER Input", s.toString());
}
#Override
public void afterTextChanged(Editable s) {}
});
}
}
}
}
}
}
The best way to achieve this
Create a custom layout which doesn't exposes methods like gettext and addTextWatcher.
Never store this data as plain string if you are storing this on the store.(Use encryption).
Recylerview
#Override
public void onBindViewHolder(#NonNull CustomListview.ViewHolder holder, int position) {
holder.Sno.setText(""+(position+1));
//remove colth unwanted characters
String cloth = list.get(position).getCloth();
String withoutFirstCharacter = cloth.substring(2,cloth.length()-1); // index starts at zero
holder.Cloth.setText(""+withoutFirstCharacter);
holder.qty.setText(list.get(position).getQuantity());
holder.QtyId.setText(list.get(position).getQtyId());
holder.Dis.setText(list.get(position).getDispatchedQty());
holder.deliverd is a edittext
if (list.get(position).getValue()==null){
holder.deliverd.setText("0");
list.get(position).setValue("0");
} else {
holder.deliverd.setText(String.valueOf(list.get(position).getValue()));
}
holder.deliverd.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (TextUtils.isEmpty(holder.deliverd.getText()))
{
list.get(position).setValue("0");
} else {
list.get(position).setValue(holder.deliverd.getText().toString());
}
int a = Integer.parseInt(list.get(position).getValue());
//str is arraylist
str.add(Integer.parseInt(list.get(position).getValue()));
Log.i( "str",String.valueOf(str));
//interface through pass the values to mainactivity
dtInterface.onSetValues(str);
// activity.array_val();
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
Interface
public interface DataTransferInterface {
public void onSetValues(ArrayList<Integer> in );
}
MAinactivity interface implements and override the interface method
#Override
public void onSetValues(ArrayList<Integer> a )
{
HashMap<Integer, ArrayList<Integer>> hm=new HashMap<>();
hm.put(100,a);
//display hashmap element
Set s = hm.entrySet();
Iterator itr = s.iterator();
while (itr.hasNext()){
Map.Entry m = (Map.Entry)itr.next();
System.out.println(m.getKey()+":values:"+m.getValue());
Toast.makeText(this, "aaa"+m.getValue(),Toast.LENGTH_SHORT).show();
}
}
when i change the 1st,2nd,3rd edittext are correctly fetched the values one time. The problem is 4th,5th,etc edittext repeating the values 2 times and added to the arraylist. how to slove it.i tried more than 25days.. i attached the error below.
[![enter image description here][1]][1]
[enter link description here][1]
https://andriodretrofit.000webhostapp.com/WhatsAppVideo2020-02-03at83032PM.gif
I want the listView to be displayed only when a search string is inserted in the EditText. Below is my code...Please help me I would be highly obliged...
public class MainActivity extends AppCompatActivity {
private ListView mSearchNFilterLv;
private EditText mSearchEdt;
private ArrayList<String> mStringList;
private ValueAdapter valueAdapter;
private TextWatcher mSearchTw;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
valueAdapter = new ValueAdapter(mStringList, this);
mSearchNFilterLv.setAdapter(valueAdapter);
mSearchEdt.addTextChangedListener(mSearchTw);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private void initData() {
mStringList = new ArrayList<String>();
mStringList.add("one");
mStringList.add("two");
mStringList.add("three");
mStringList.add("four");
mStringList.add("five");
mStringList.add("six");
mStringList.add("seven");
mStringList.add("eight");
mStringList.add("nine");
mStringList.add("ten");
mStringList.add("eleven");
mStringList.add("twelve");
mStringList.add("thirteen");
mStringList.add("fourteen");
mSearchTw = new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
valueAdapter.getFilter().filter(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if(mSearchEdt.getVisibility() != View.VISIBLE)
mSearchEdt.setVisibility(View.VISIBLE);
}
};
}
private void initUI() {
mSearchNFilterLv = (ListView) findViewById(R.id.list_view);
mSearchEdt = (EditText) findViewById(R.id.txt_search);
}
}
You can use the code below:
mSearchTw = new TextWatcher() {
#Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s != null && s.toString().trim().length() > 0) {
mSearchNFilterLv.setVisibility(View.VISIBLE);
} else {
mSearchNFilterLv.setVisibility(View.GONE);
}
}
#Override public void afterTextChanged(Editable s) {
}
};
This will show the ListView when there is an entered text (while entering text to EditText) and if text has 1 or more characters.
If you want ListView to be changed only when the insertion is over move the given code inside onTextChanged to afterTextChanged
PS: You can use the same structure with SearchView instead of EditText with query listeners.
Updated
You can use below code to hide ListView initially:
private void initUI() {
mSearchNFilterLv = (ListView) findViewById(R.id.list_view);
mSearchEdt = (EditText) findViewById(R.id.txt_search);
mSearchNFilterLv.setVisibility(View.GONE);
}
Having problems getting Fragment to Activity communication working properly..
I have an edit box in a Fragment and I need it to send its value back to the main activity after each edit.
Fragment Class:
public class FragA extends Fragment {
FragAInt mCallback;
public interface FragAInt{
public void onEditBoxEdited(String boxContent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
final View et = view.findViewById(R.id.eboxa) ;
((EditText) et).addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mCallback.onEditBoxEdited(((EditText) et).getText().toString());
}
});
return view;
}
}
MainActivity
public class MainActivity extends FragmentActivity implements FragA.FragAInt{
private String editBoxVal;
public void onEditBoxEdited(String editBoxContent){
this.editBoxVal= editBoxContent;
}
}
The app will run but it will crash as soon as you type in the exit box with a null pointer exception.
Where have I gone wrong?
Full error:
java.lang.NullPointerException:
Attempt to invoke interface method 'void nemo.myapplication.FragA$FragAInt.onEditBoxEdited(java.lang.String)' on a null object reference
at nemo.myapplication.FragA$1.afterTextChanged(FragA.java:49)
at android.widget.TextView.sendAfterTextChanged(TextView.java:8017)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:10182)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1043)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:560)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:492)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:34)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:685)
at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:445)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:340)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)
You need to assign the mCallback variable. I think you missed that part, you can use onAttach event.
public class FragA extends Fragment {
FragAInt mCallback;
public interface FragAInt{
public void onEditBoxEdited(String boxContent);
}
public FragA(FragAInt mCallback) {
this.mCallback = mCallback;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (FragAInt) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
final View et = view.findViewById(R.id.eboxa) ;
((EditText) et).addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
mCallback.onEditBoxEdited(((EditText) et).getText().toString());
}
});
return view;
}
}
Why is it not possible to call the checkButtonAddmethod in my Fragment?
public class AddDataFragment extends Fragment {
DbHelper mydb;
Button buttonadd;
Button buttondelete;
private EditText inputLabel;
//Validator boolean
public boolean labelOk;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_data, container, false);
final Context context = getContext();
mydb = new DbHelper(context);
[...]
//Label
inputLabel = (EditText) view.findViewById(R.id.editText_label);
inputLabel.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
Pattern p = Pattern.compile("^[A-Z]{3}-[0-9]{4}$");
Matcher m = p.matcher(s);
labelOk = m.find();
Log.d("ButtonAdd?", String.valueOf(labelOk));
}
checkButtonAdd(); // <---------------
});
[...]
return view;
}
public void checkButtonAdd(){
if (labelOk){
buttonadd.setEnabled(true);
}else{
buttonadd.setEnabled(false);
}
};
}
I thought this should work because I used it in another Application. But in the MainActivitiy could this be the mistake and if yes, how can I solve it?
You have to call that method from one of the callback methods using this keyword, to get the instance of enclosing Fragment. For instance..
#Override
public void afterTextChanged(Editable s) {
// Your Statements
AddDataFragment.this.checkButtonAdd();
}
No need to declare the method as static. In fact, declaring it static will be a piece of poor code
You are not in the right scope.
Try
AddDataFragment.checkButtonAdd();
instead.