Java 片段中的自动完成文本
我正在尝试创建一个片段式的自动完成文本,我看到了很多例子,但对我来说不起作用。。。我总是在Java 片段中的自动完成文本,java,android,android-fragments,autocompletetextview,Java,Android,Android Fragments,Autocompletetextview,我正在尝试创建一个片段式的自动完成文本,我看到了很多例子,但对我来说不起作用。。。我总是在适配器上遇到同样的错误。我的代码上的东西没有出现任何错误,但当我启动我的应用程序并单击片段时,它崩溃了 我的LlistaGenericaFragment.java看起来像: public class LlistaGenericaFragment extends Fragment { String[] totselements; String[] imatgeselements; public Llist
适配器上遇到同样的错误。我的代码上的东西没有出现任何错误,但当我启动我的应用程序并单击片段时,它崩溃了
我的LlistaGenericaFragment.java看起来像:
public class LlistaGenericaFragment extends Fragment {
String[] totselements;
String[] imatgeselements;
public LlistaGenericaFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.llista_generica_fragment, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
totselements = getResources().getStringArray(R.array.tots);
imatgeselements = getResources().getStringArray(R.array.imagenestots);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<24;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", totselements[i]);
hm.put("flag", (imatgeselements[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "imatge","text"};
// Ids of views in listview_layout
int[] to = { R.id.imatgeelement,R.id.textelement};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.llista_generica_fragment, from, to);
// Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file
CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) getActivity().findViewById(R.id.autocomplete);
/** Defining an itemclick event listener for the autocompletetextview */
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
/** Each item in the adapter is a HashMap object.
* So this statement creates the currently clicked hashmap object
* */
HashMap<String, String> hm = (HashMap<String, String>) arg0.getAdapter().getItem(position); // UNCHECKED CAST ??????
}
};
/** Setting the adapter to the listView */
autoComplete.setAdapter(adapter);
}
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/** Returns the country name corresponding to the selected item */
@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
/** Each item in the autocompetetextview suggestion list is a hashmap object */
HashMap<String, String> hm = (HashMap<String, String>) selectedItem; // <-- UNCHEKED CAST ????????
return hm.get("txt");
}
其中joancolmenero.taulaperiodica.com.taulaperiodicapp.LlistaGenericaFragment.onCreate(LlistaGenericaFragment.java:100)
指向autoComplete.setAdapter(adapter)代码>
当我为getView()更改getActivity()时,它不会崩溃,但会显示:
02-01 17:51:49.118 10048-11634/joancolmenero.taulaperiodica.com.taulaperiodicaapp W/Filter﹕ An exception occured during performFiltering()!
java.lang.NullPointerException
at android.widget.SimpleAdapter$SimpleFilter.performFiltering(SimpleAdapter.java:354)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
希望您能帮助我。如果AutoCompleteTextView
是您在onCreateView
中返回的布局的一部分,则调用getActivity()。findViewById
insinde onCreate of your Fragment子类,将返回空对象,因为片段尚未附加到活动的视图层次结构。在您的情况下,最简单的修复方法是将逻辑移到activitycreated
(在onCreateView
之后调用)中,然后使用getView().findViewById
查找属于您在onCreateView
中返回的布局的视图,如果AutoCompleteTextView
是您在onCreateView
中返回的布局的一部分,则调用getActivity()。findViewById
在片段子类的Inde onCreate中,将返回空对象,因为片段尚未附加到活动的视图层次结构。在您的情况下,最简单的修复方法是将逻辑移到activitycreated(在onCreateView
之后调用)中,然后使用getView().findViewById
查找属于您在onCreateView
中返回的布局的视图为什么CustomAutoCompleteTextView在活动布局中而不是在片段布局中?为什么CustomAutoCompleteTextView在活动布局中而不是在片段布局中?那么您认为如果我为onActivityCreated它会工作吗?OnCreateforOnActivityCreated,并使用getView代替getActivityyou正在使用的String[]from={“imatge”,“text”}
而不是String[]from={“flag”,“txt”}代码>这是一个错误,但当我键入某个内容时,它会显示与我的文本视图相同的提示,我无法在这方面帮助您。只有您知道您正在使用的数据集的内容。您认为如果我更改onActivityCreated的onCreateView,它会工作吗?OnCreateForOnActivityCreated,并使用getView代替GetActivity您正在使用的String[]from={“imatge”,“text”}
而不是String[]from={“flag”,“txt”}代码>这是一个错误,但当我键入某个内容时,它会显示与我的文本视图相同的提示,我无法在这方面帮助您。只有您知道您正在使用的数据集的内容如何
02-01 17:51:49.118 10048-11634/joancolmenero.taulaperiodica.com.taulaperiodicaapp W/Filter﹕ An exception occured during performFiltering()!
java.lang.NullPointerException
at android.widget.SimpleAdapter$SimpleFilter.performFiltering(SimpleAdapter.java:354)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)