我为平板电脑制作了一款适用于Android 3.2的应用程序。现在我试图让它在android 4+上工作,但不知何故,代码的一小部分不起作用。我是一名初学者,所以我不知道自从代码是从前一段时间的代码中获取以来还没有起作用。
崩溃的代码部分只是一个文本框,您可以在其中根据微调器编写任何内容,您可以从中选择要从中筛选的数据库中的哪个字段。它在3.2上运行完美(同样的代码片段在我在两个版本的android上的另一个片段上的其他一些微调器中工作)。
首先,我们使用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)
这是崩溃((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) {
}
});*/
任何有可能崩溃的想法?
提前谢谢。答案 0 :(得分:0)
空指针异常表示某些内容未初始化。在这种情况下,我强烈怀疑它是text_search
。尝试使用Log.d('TAG',text_search)
将其记录下来,看看它是否实际为空。我怀疑发生的事情是你在EditText text_search = (EditText)getActivity().findViewById(R.id.text_search);
上面的行中的文件名中有拼写错误,或者你从未夸大布局。