所以我一直在关注教程here并添加了几个东西。当我尝试运行应用程序时,我得到一个nullpointer错误,说
rowItems.add(item);
我将假设意味着项目为空,但我之前设置了项目
item = new RowItem(id,image,name,distance,date,time,dist_display,timeAway);
现在这些都不应该为null(进入RowItem的项目)。
我不知道从哪里开始搞清楚,所以任何帮助都将不胜感激!如果我需要添加更多代码以显示更多正在发生的事情,请告诉我。
提前谢谢你,
泰勒
编辑:
02-13 13:30:29.406: E/AndroidRuntime(26261): FATAL EXCEPTION: AsyncTask #2
02-13 13:30:29.406: E/AndroidRuntime(26261): Process: com.skateconnect, PID: 26261
02-13 13:30:29.406: E/AndroidRuntime(26261): java.lang.RuntimeException: An error occured while executing doInBackground()
02-13 13:30:29.406: E/AndroidRuntime(26261): at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.lang.Thread.run(Thread.java:841)
02-13 13:30:29.406: E/AndroidRuntime(26261): Caused by: java.lang.NullPointerException
02-13 13:30:29.406: E/AndroidRuntime(26261): at com.skateconnect.AllSpotsActivity$LoadAllSpots.doInBackground(AllSpotsActivity.java:513)
02-13 13:30:29.406: E/AndroidRuntime(26261): at com.skateconnect.AllSpotsActivity$LoadAllSpots.doInBackground(AllSpotsActivity.java:1)
02-13 13:30:29.406: E/AndroidRuntime(26261): at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-13 13:30:29.406: E/AndroidRuntime(26261): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-13 13:30:29.406: E/AndroidRuntime(26261): ... 3 more
02-13 13:30:29.936: E/WindowManager(26261): android.view.WindowLeaked: Activity com.skateconnect.AllSpotsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42151fb0 V.E..... R.....ID 0,0-684,192} that was originally added here
02-13 13:30:29.936: E/WindowManager(26261): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
02-13 13:30:29.936: E/WindowManager(26261): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
02-13 13:30:29.936: E/WindowManager(26261): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-13 13:30:29.936: E/WindowManager(26261): at android.app.Dialog.show(Dialog.java:286)
02-13 13:30:29.936: E/WindowManager(26261): at com.skateconnect.AllSpotsActivity$LoadAllSpots.onPreExecute(AllSpotsActivity.java:376)
02-13 13:30:29.936: E/WindowManager(26261): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
02-13 13:30:29.936: E/WindowManager(26261): at android.os.AsyncTask.execute(AsyncTask.java:535)
02-13 13:30:29.936: E/WindowManager(26261): at com.skateconnect.AllSpotsActivity$3.onClick(AllSpotsActivity.java:257)
02-13 13:30:29.936: E/WindowManager(26261): at android.view.View.performClick(View.java:4452)
02-13 13:30:29.936: E/WindowManager(26261): at android.view.View$PerformClick.run(View.java:18498)
02-13 13:30:29.936: E/WindowManager(26261): at android.os.Handler.handleCallback(Handler.java:733)
02-13 13:30:29.936: E/WindowManager(26261): at android.os.Handler.dispatchMessage(Handler.java:95)
02-13 13:30:29.936: E/WindowManager(26261): at android.os.Looper.loop(Looper.java:137)
02-13 13:30:29.936: E/WindowManager(26261): at android.app.ActivityThread.main(ActivityThread.java:5083)
02-13 13:30:29.936: E/WindowManager(26261): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 13:30:29.936: E/WindowManager(26261): at java.lang.reflect.Method.invoke(Method.java:515)
02-13 13:30:29.936: E/WindowManager(26261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-13 13:30:29.936: E/WindowManager(26261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-13 13:30:29.936: E/WindowManager(26261): at dalvik.system.NativeStart.main(Native Method)
第513行是rowItems.add(item);
EDIT2:
@Override
protected String doInBackground(String... args) {
int n=0;
search_trig=0;
String type=null;
String date=null;
String time=null;
int image;
RowItem item=null;
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Spots: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
if(add.equals("Event")){
spots = json.getJSONArray("events");
}else{
spots = json.getJSONArray(TAG_SPOTS);
}
// looping through All Products
for (int i = 0; i < spots.length(); i++) {
JSONObject c = spots.getJSONObject(i);
search_trig=0;
// Storing each json item in variable
String strlong = c.getString(TAG_LONG);
double elong = Double.parseDouble(strlong);
String strlat = c.getString(TAG_LAT);
double elat = Double.parseDouble(strlat);
double dist = getDistance(slat, slong, elat, elong);
String distance = String.format("%.1f", dist);
String id = c.getString(TAG_PID);
String pave = c.getString(TAG_PAVEMENT);
String traffic = c.getString(TAG_TRAFFIC);
String enviro = c.getString(TAG_ENVIRONMENT);
String name = c.getString(TAG_NAME);
String image_B64 = c.getString(TAG_IMAGE_B64);
String dist_display = distance+ " Mi";
String timeAway=".";
type=".";
date=".";
time=".";
//EVENT STUFF
if(add.equals("Event")){
//need to add other stuff too
type = c.getString(TAG_TYPE);
date = c.getString(TAG_DATE);
time = c.getString(TAG_TIME);
timeAway = String.valueOf(getTimeAWAY(date));
}
image = getDispImage(type,enviro);
if(search_bp==1){
//checks distance
if(str_dist.equals("Any")==false){
if((dist>Double.parseDouble(str_dist))){
search_trig=1;
}
}
if(str_pavement.equals("Any")==false){
if((str_pavement.equals(pave)==false)){
search_trig=1;
}
}
if(str_traffic.equals("Any")==false){
if((str_traffic.equals(traffic)==false)){
search_trig=1;
}
}
if(str_enviro.equals("Any")==false){
if((str_enviro.equals(enviro)==false)){
search_trig=1;
}
}
//EVENT STUFF
if(add.equals("Event")){
if(str_type.equals("Any")==false){
if((str_type.equals(type)==false)){
search_trig=1;
}
}
}
item = new RowItem(id,image,name,distance,date,time,dist_display,timeAway);
shr_spots.put(id, new SHARE(id, name,strlong,strlat,pave,traffic,enviro,image_B64,date,time,type));
if(search_trig == 0){
//spotsList.add(map);
rowItems.add(item);
n=1;
}
}else{
//spotsList.add(map);
rowItems.add(item);
n=1;
}
}
if(n==0){
Intent intent = getIntent();
finish();
startActivity(intent);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "No "+add+" found with those specifications.", Toast.LENGTH_SHORT).show();
}
});
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewSpotActivity.class);
finish();
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
513是第二个
rowItems.add(item);
正因为我按下了哪个按钮,如果我按下另一个按钮那么它就是第一个给出错误的按钮。