NumenorForLife
NumenorForLife

Reputation: 1746

AutoCompleteTextView NullPointer in Button's OnClickListener

A NullPointerException is thrown when I attempt to extract the text from an AutoCompleteTextView upon hitting a button in a Fragment. This is my code...

public class TreeMenuFragment extends SherlockFragment{
protected View view; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    view = inflater.inflate(R.layout.treemenu, container, false);
    //Auto-Complete
    AutoCompleteTextView autoView = (AutoCompleteTextView) view.findViewById(R.id.edit_message);
    String[] itemOptions = getResources().getStringArray(R.array.edit_message);

    ArrayAdapter<String> theAdapter = 
            new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemOptions);
    autoView.setAdapter(theAdapter);
    ((BaseAdapter) autoView.getAdapter()).notifyDataSetChanged();
    ((ViewGroup) autoView.getParent()).removeView(autoView);

    container.addView(autoView);

    Button b = (Button) view.findViewById(R.id.post_blacklist_button);
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message);
            String blackListItem = editText.getText().toString();

            editText.setText("");
            MainActivity.getInstance().postBlackListItem(blackListItem);

        }
    });
    return view;
}

}

The relevant xml is here:

    <AutoCompleteTextView
    android:id="@+id/edit_message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/my_hint" />

<Button
    android:id="@+id/post_blacklist_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/update_button" />

*Update:*When I attempted to change the AutoCompleteTextViev cast from v to View, my log prints the following:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.privacyapp.TreeMenuFragment$1.onClick(TreeMenuFragment.java:34)
at android.view.View.performClick(View.java:2532)
at android.view.View$PerformClick.run(View.java:9293)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4263)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 798

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Change

AutoCompleteTextView editText=(AutoCompleteTextView) v.findViewById(R.id.edit_message);

to

AutoCompleteTextView editText=(AutoCompleteTextView) view.findViewById(R.id.edit_message);

Here v is the Button which you clicked and I'm assuming that view is the inflated xml which is where you will find the AutoCompleteTextView. So this means that your editText is null and throws a NPE when you try to call a function on it like

     editText.getText().toString();

If this doesn't fix the problem then please post logcat but this line will still need to be changed

Declare autoView as a member variable then you only have to initialize it once. Then use that instance of your AutoCompleteTextView inside your onClick to get the text

Upvotes: 1

Related Questions