I made an app for tablets that is working just fine on android 3.2. Now I am trying to make it work on android 4+ but somehow a tiny part of the code is not working. I am a beginner so I just can´t figure out what isn´t working since the code was taken from an example some time ago.
The part of the code that is crashing is just an text box where you can write whatever you want based on an spinner where you select what field from the database you wanna filter from. It works flawlessly on 3.2 (and the same piece of code is working in some other spinners I have on another fragments on both versions of android).
First here we go with the logcat:
11-12 18:33:00.428: E/AndroidRuntime(8140): FATAL EXCEPTION: main
11-12 18:33:00.428: E/AndroidRuntime(8140): java.lang.NullPointerException
11-12 18:33:00.428: E/AndroidRuntime(8140): at dvgvsc.vscdvg.Fragmentsearchlist.ifacesearch(Fragmentsearchlist.java:159)
11-12 18:33:00.428: E/AndroidRuntime(8140): at dvgvsc.vscdvg.Fragmentsearchlist.onPrepareOptionsMenu(Fragmentsearchlist.java:103)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl.dispatchPrepareOptionsMenu(FragmentManager.java:1865)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.Activity.onPreparePanel(Activity.java:2463)
11-12 18:33:00.428: E/AndroidRuntime(8140): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:438)
11-12 18:33:00.428: E/AndroidRuntime(8140): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:761)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.Activity.invalidateOptionsMenu(Activity.java:2552)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl.invalidateOptionsMenu(FragmentManager.java:1914)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.Fragment.setHasOptionsMenu(Fragment.java:898)
11-12 18:33:00.428: E/AndroidRuntime(8140): at dvgvsc.vscdvg.Fragmentsearchlist.onCreateView(Fragmentsearchlist.java:47)
11-12 18:33:00.428: E/AndroidRuntime(8140): at dvgvsc.vscdvg.OrdenesFragment.onCreateView(OrdenesFragment.java:65)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.BackStackRecord.run(BackStackRecord.java:622)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.os.Handler.handleCallback(Handler.java:605)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.os.Looper.loop(Looper.java:137)
11-12 18:33:00.428: E/AndroidRuntime(8140): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-12 18:33:00.428: E/AndroidRuntime(8140): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 18:33:00.428: E/AndroidRuntime(8140): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 18:33:00.428: E/AndroidRuntime(8140): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-12 18:33:00.428: E/AndroidRuntime(8140): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-12 18:33:00.428: E/AndroidRuntime(8140): at dalvik.system.NativeStart.main(Native Method)
And here is the piece of code that is crashing ((Line: text_search.addTextChangedListener(new TextWatcher() { ))
private final void ifacesearch() {
/** search box that is used on the searches, using a listerner which will move the cursor based on the user imput. */
/*EditText text_search = (EditText)getActivity().findViewById(R.id.text_search);
text_search.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable text) {
if (text.length() > 0) {
adapter.changeCursor(vscdvgActivity._db.defcursor(TABLA, SEARCH_FILTER, text.toString()));
}else{
adapter.changeCursor(vscdvgActivity._db.defcursor(TABLA, null, null));
}
}
public void beforeTextChanged(CharSequence text, int start, int before, int count) {
}
public void onTextChanged(CharSequence text, int start, int before, int count) {
}
});*/
/** add to the spinner the db fields /*
/*Spinner spinner_filter = (Spinner)getActivity().findViewById(R.id.spinner_filter);
ArrayAdapter<String> spinner_adapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_spinner_dropdown_item,
COLUMN_FILTER[1]);
spinner_filter.setAdapter(spinner_adapter);
spinner_filter.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
SEARCH_FILTER = COLUMN_FILTER[0][position];
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});*/
Any ideas what could be possibily crashing?
thanks in advance.
Null pointer exception means that something wasn’t initialized. In this case, I strongly suspect it is
text_search. Try logging it out with aLog.d('TAG',text_search), see if it is in fact null. I suspect what is happening is that you either have a typo in the name of the file in the line aboveEditText text_search = (EditText)getActivity().findViewById(R.id.text_search);, or you never inflated the layout.