所以,我正在开发一个只有一个活动和两个片段的项目。其中一个片段有一个使用自定义适配器和模型填充的lisView。
我必须根据搜索实现搜索机制并更改列表项。
截至目前,我开始了解两种方法: 1.使用QueryListeners和所有的SearchView。 2.带有textChange侦听器的EditText和所有。
我的适配器类是:
public class CityAdapter extends ArrayAdapter<City> {
private Context context;
private List<City> objects;
public CityAdapter(Context context, int resource, List<City> objects) {
super(context, resource, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
City city = objects.get(position);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.state_list_item, null);
ImageView image = (ImageView) view.findViewById(R.id.cityPic);
image.setImageResource(city.getPicResource());
TextView tv = (TextView) view.findViewById(R.id.cityName);
tv.setText(city.getCityName());
return view;
}
包含虚拟数据的我的数据输入类:
public class CityData {
private List<City> city = new ArrayList<City>();
public List<City> getCity() {
return city;
}
public CityData() {
city.add(new City("New Delhi", R.drawable.ic_launcher,
"Large double. Good grower, heavy bloomer."));
city.add(new City("Tibouchina Semidecandra", R.drawable.ic_launcher,
"Beautiful large royal purple flowers."));
city.add(new City("Hibiscus", R.drawable.ic_launcher,
"Blooms in summer, 20-35 inches high."));
city.add(new City("New Delhi", R.drawable.ic_launcher,
"Large double. Good grower, heavy bloomer."));
city.add(new City("Tibouchina Semidecandra", R.drawable.ic_launcher,
"Beautiful large royal purple flowers."));
city.add(new City("Hibiscus", R.drawable.ic_launcher,
"Blooms in summer, 20-35 inches high."));
}
My Fragment类(将包含listView和EditText):
public class SearchFragment extends Fragment {
List<City> city= new CityData().getCity();
private EditText editText;
private ListView listView;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<City> arrayOfUsers = new ArrayList<City>();
CityAdapter adapter = new CityAdapter(context, R.layout.state_item_list, arrayOfUsers);
listView.setAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.search, null, false);
//return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
在我的主要活动中,我给片段充气。
现在问题:
我希望使用此类型的建模数据为我的列表执行搜索功能所需的方法。一个列表项将有一个图片和标题, 当用户点击列表项时,事实描述将被发送到另一个片段。 此应用程序崩溃。我是Android开发新手,我确信我犯了愚蠢的错误。
提前致谢!
![logcat的] [1]
02-19 08:23:57.223 1973-1973/com.iwillcode.searchandfragments E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.iwillcode.searchandfragments, PID: 1973
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iwillcode.searchandfragments/com.iwillcode.searchandfragments.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.iwillcode.searchandfragments.CityModelling.CityAdapter.<init>(CityAdapter.java:28)
at com.iwillcode.searchandfragments.FragmentClasses.SearchFragment.onActivityCreated(SearchFragment.java:75)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
at android.app.Activity.performStart(Activity.java:5949)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)