private List<Product> itemList;
public List<Product> getItemList() {
return itemList;
}
然后尝试接受这样......
public void doMySearch (String query){
System.out.println("bikaMiden");
List<Product> searchResult = new ArrayList<Product>();
List<Product> result = new ArrayList<Product>();
System.out.println("bikagamwwwwwwwww");
result= adapter.getItemList();
System.out.println("bikaMIA");
for (Product product : result) {
System.out.println("bika");
if (product.getProductName().contains(query)) {
searchResult.add(product);
System.out.println("vrika" + product);
}
}
adapter.setItemList(searchResult);
adapter.notifyDataSetChanged();
}
我已经尝试了50种不同的aproach它总是在分配给结果命令时崩溃,如果你知道帮助我...
02-27 21:51:58.847: D/AndroidRuntime(3194): Shutting down VM
02-27 21:51:58.857: W/dalvikvm(3194): threadid=1: thread exiting with uncaught exception (group=0x41465700)
02-27 21:51:58.897: E/AndroidRuntime(3194): FATAL EXCEPTION: main
02-27 21:51:58.897: E/AndroidRuntime(3194): java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.ac.kent.jhp20.shopper/uk.ac.kent.jhp20.shopper.ProductListActivity}: java.lang.NullPointerException
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.os.Looper.loop(Looper.java:137)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-27 21:51:58.897: E/AndroidRuntime(3194): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 21:51:58.897: E/AndroidRuntime(3194): at java.lang.reflect.Method.invoke(Method.java:525)
02-27 21:51:58.897: E/AndroidRuntime(3194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-27 21:51:58.897: E/AndroidRuntime(3194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-27 21:51:58.897: E/AndroidRuntime(3194): at dalvik.system.NativeStart.main(Native Method)
02-27 21:51:58.897: E/AndroidRuntime(3194): Caused by: java.lang.NullPointerException
02-27 21:51:58.897: E/AndroidRuntime(3194): at uk.ac.kent.jhp20.shopper.ProductListActivity.doMySearch(ProductListActivity.java:139)
02-27 21:51:58.897: E/AndroidRuntime(3194): at uk.ac.kent.jhp20.shopper.ProductListActivity.onCreate(ProductListActivity.java:115)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.Activity.performCreate(Activity.java:5133)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-27 21:51:58.897: E/AndroidRuntime(3194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-27 21:51:58.897: E/AndroidRuntime(3194): ... 11 more
02-27 21:57:00.157: D/dalvikvm(3221): GC_FOR_ALLOC freed 49K, 8% free 2778K/2988K, paused 43ms, total 47ms
02-27 21:57:00.177: I/dalvikvm-heap(3221): Grow heap (frag case) to 3.939MB for 1127536-byte allocation
02-27 21:57:00.217: D/dalvikvm(3221): GC_FOR_ALLOC freed 2K, 6% free 3876K/4092K, paused 39ms, total 39ms
02-27 21:57:00.408: D/libEGL(3221): loaded /system/lib/egl/libEGL_emulation.so
02-27 21:57:00.418: D/(3221): HostConnection::get() New Host Connection established 0x2a1bdc30, tid 3221
02-27 21:57:00.437: D/libEGL(3221): loaded /system/lib/egl/libGLESv1_CM_emulation.so
02-27 21:57:00.448: D/libEGL(3221): loaded /system/lib/egl/libGLESv2_emulation.so
02-27 21:57:00.489: W/EGL_emulation(3221): eglSurfaceAttrib not implemented
02-27 21:57:00.489: D/OpenGLRenderer(3221): Enabling debug mode 0
02-27 21:57:00.538: D/dalvikvm(3221): GC_FOR_ALLOC freed 8K, 5% free 4259K/4444K, paused 27ms, total 27ms
02-27 21:57:00.627: D/dalvikvm(3221): GC_FOR_ALLOC freed 1K, 4% free 4614K/4796K, paused 20ms, total 20ms
02-27 21:57:00.707: D/dalvikvm(3221): GC_FOR_ALLOC freed <1K, 4% free 4969K/5148K, paused 21ms, total 22ms
02-27 21:57:00.797: I/Choreographer(3221): Skipped 30 frames! The application may be doing too much work on its main thread.
这里是logcat ...请帮助..我正在构建一个Android项目,如果这个问题很愚蠢的话,我真的很新。 更新适配器不为null我的oncreate方法在这里项目相当庞大所以我不得不选择一些部分......
private ProductListAdapter adapter;
private ListViewLoader loader;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//get the category form the intent
Intent intent = getIntent();
/*
* To here
*/
adapter = new ProductListAdapter(this);
setListAdapter(adapter);
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
System.out.println("bika");
/*
* because you refer to it here
*/
doMySearch(query);
System.out.println("ti paixtike edw malakako?");
}
else
{
categoryId = intent.getIntExtra(MainActivity.SELECTED_CATEGORY, 1);
/*
* Move this
*
adapter = new ProductListAdapter(this);
setListAdapter(adapter);
*
*/
loader = new ListViewLoader(adapter, categoryId);
loader.execute(String.format(MainActivity.WEBSERVER_GETLIST, categoryId));
}
setupActionBar();
}
答案 0 :(得分:0)
适配器为null,因此无法调用getItemList()!这会导致NullPointerException。
检查适配器是否为空。
答案 1 :(得分:0)
很难看出你的null可能在哪里,但正如Edwin T已经指出它可能是适配器对象。但是我建议在这种情况下对适配器和项目结果进行空检查。
这样的事情:
public void doMySearch(String query){
if (adapter != null) {
List<Product> searchResult = new ArrayList<Product>();
List<Product> result = new ArrayList<Product>();
result = adapter.getItemList();
if (result != null) {
for (Product product : result) {
if (product.getProductName().contains(query)) {
searchResult.add(product);
}
}
adapter.setItemList(searchResult);
adapter.notifyDataSetChanged();
}
}
}