Android应用程序意外停止

时间:2013-01-15 17:29:51

标签: android

我正在做一个使用GPS检索用户位置然后将其重定向到新意图的应用程序,假设用户处于“曲线”状态,它会将用户重定向到Curve.class,但我得到了这个错误,它一直阻止我继续前进。

01-15 17:21:09.644: D/HttpRetriever(309): <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ResultSet xmlns:ns1="http://www.yahooapis.com/v1/base.rng" version="2.0" xml:lang="en-US"><Error>0</Error><ErrorMessage>No error</ErrorMessage><Locale>en-US</Locale><Found>1</Found><Quality>99</Quality><Result><quality>72</quality><latitude>3.158695</latitude><longitude>101.611586</longitude><offsetlat>3.158695</offsetlat><offsetlon>101.611586</offsetlon><radius>400</radius><name>3.1580999999999997, 101.61209833333332</name><line1>Jalan Pju 7/3</line1><line2>47800 Bandar Utama</line2><line3></line3><line4>Malaysia</line4><house></house><street>Jalan Pju 7/3</street><xstreet></xstreet><unittype></unittype><unit></unit><postal>47800</postal><neighborhood>Bandar Utama</neighborhood><city>Sungai Buloh</city><county>Petaling</county><state>Selangor</state><country>Malaysia</country><countrycode>MY</countrycode><statecode></statecode><countycode></countycode><uzip>47800</uzip><hash></hash><woeid>28347311</woeid><woetype>11</woetype></Result></ResultSet>
01-15 17:21:09.644: D/HttpRetriever(309): <!-- nws01.maps.bf1.yahoo.com uncompressed/chunked Tue Jan 15 09:21:01 PST 2013 -->
01-15 17:21:09.644: D/HttpRetriever(309): <!-- wws07.geotech.ne1.yahoo.com uncompressed/chunked Tue Jan 15 09:21:00 PST 2013 -->
01-15 17:21:09.733: D/dalvikvm(309): GC_FOR_MALLOC freed 2299 objects / 157648 bytes in 58ms
01-15 17:21:09.754: D/XmlParser(309): Location:-Jalan Pju 7/3-47800 Bandar Utama-Malaysia
01-15 17:21:10.134: D/AndroidRuntime(309): Shutting down VM
01-15 17:21:10.134: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 17:21:10.157: E/AndroidRuntime(309): FATAL EXCEPTION: main
01-15 17:21:10.157: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.javacodegeeks.android.lbs/com.javacodegeeks.android.lbs.Curve}: java.lang.NullPointerException
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.os.Looper.loop(Looper.java:123)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 17:21:10.157: E/AndroidRuntime(309):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 17:21:10.157: E/AndroidRuntime(309):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 17:21:10.157: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 17:21:10.157: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 17:21:10.157: E/AndroidRuntime(309):  at dalvik.system.NativeStart.main(Native Method)
01-15 17:21:10.157: E/AndroidRuntime(309): Caused by: java.lang.NullPointerException
01-15 17:21:10.157: E/AndroidRuntime(309):  at com.javacodegeeks.android.lbs.Curve.onCreate(Curve.java:59)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-15 17:21:10.157: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-15 17:21:10.157: E/AndroidRuntime(309):  ... 11 more
01-15 17:21:11.204: D/All Products:(309): {"success":1,"daftar_promotion":[{"id":"26","PromoImage":"\/images\/ego1.jpg","PromoCate":"Health  ","PromoDesc":"5% discount for items above RM200","PromoDateStart":"1-1-2013","Contact":"","PromoDateEnd":"31-1-2013","PromoName":"Egonutritions 5%  "},{"id":"27","PromoImage":"\/images\/dna.png","PromoCate":"Fashion","PromoDesc":"20% off for all shoes","PromoDateStart":"1-1-2013","Contact":"","PromoDateEnd":"31-1-2013","PromoName":"DNA New Year Sales"}]}
01-15 17:21:11.204: W/System.err(309): org.json.JSONException: No value for PromoID
01-15 17:21:11.404: W/System.err(309):  at org.json.JSONObject.get(JSONObject.java:354)
01-15 17:21:11.484: W/System.err(309):  at org.json.JSONObject.getString(JSONObject.java:510)
01-15 17:21:11.484: W/System.err(309):  at com.javacodegeeks.android.lbs.Curve$Activity.doInBackground(Curve.java:105)
01-15 17:21:11.484: W/System.err(309):  at com.javacodegeeks.android.lbs.Curve$Activity.doInBackground(Curve.java:1)
01-15 17:21:11.484: W/System.err(309):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
01-15 17:21:11.484: W/System.err(309):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-15 17:21:11.484: W/System.err(309):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-15 17:21:11.484: W/System.err(309):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
01-15 17:21:11.484: W/System.err(309):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
01-15 17:21:11.534: W/System.err(309):  at java.lang.Thread.run(Thread.java:1096)

这是我的Curve.java

package com.javacodegeeks.android.lbs;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
import lbs.promotion.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Curve extends Activity {

    private ProgressDialog pDialog;
     JSONParser jParser = new JSONParser();
     ArrayList<HashMap<String, String>> DaftarPromotion = new ArrayList<HashMap<String, String>>();

     private static String url_daftar_promotion = "http://10.0.2.2/webserver_maaug/promotion/daftar_promotion.php";

     public static final String TAG_SUCCESS = "success";
     public static final String TAG_DAFTAR_PROMOTION = "daftar_promotion";
     public static final String TAG_ID_PROMO = "PromoID";
     public static final String TAG_NAMA_PROMO = "PromoName";
     public static final String TAG_LINK_IMAGE_PROMO = "PromoImage";
     public static final String TAG_DESC_PROMO= "PromoDesc";
     public static final String TAG_CATE_PROMO = "PromoCate";

     JSONArray daftar_promotion = null;

     ListView list;
     PromotionListAdapter adapter;

     private Curve activity;

     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     DaftarPromotion = new ArrayList<HashMap<String, String>>();

      new Activity().execute();
      activity = this;
      list = (ListView) findViewById(R.id.list);
      list.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {
      }
      });
      }

      public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
      adapter = new PromotionListAdapter(activity, daftar);
      list.setAdapter(adapter);
      }

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == 100) {
      Intent intent = getIntent();
      finish();
      startActivity(intent);
      }
      }

      class Activity extends AsyncTask<String, String, String> {

      @Override
      protected void onPreExecute() {
      super.onPreExecute();
      pDialog = new ProgressDialog(Curve.this);
      pDialog.setMessage("Please Wait...");
      pDialog.setIndeterminate(false);
      pDialog.setCancelable(false);
      pDialog.show();
      }

      protected String doInBackground(String... args) {
      List<NameValuePair> params = new ArrayList<NameValuePair>();
      JSONObject json = jParser.makeHttpRequest(url_daftar_promotion, "GET",params);

      Log.d("All Products: ", json.toString());
      try {
      int success = json.getInt(TAG_SUCCESS);
      if (success == 1) {
           daftar_promotion = json.getJSONArray(TAG_DAFTAR_PROMOTION);
           for (int i = 0; i < daftar_promotion.length();i++){
           JSONObject c = daftar_promotion.getJSONObject(i);
           String PromoID = c.getString(TAG_ID_PROMO);
           String PromoName = c.getString(TAG_NAMA_PROMO);
           String PromoImage = c.getString(TAG_LINK_IMAGE_PROMO);
           String PromoDesc = c.getString(TAG_DESC_PROMO);
           String PromoCate = c.getString(TAG_CATE_PROMO);

           HashMap<String, String> map = new HashMap<String, String>();

           map.put(TAG_ID_PROMO, PromoID);
           map.put(TAG_NAMA_PROMO, PromoName);
           map.put(TAG_LINK_IMAGE_PROMO, PromoImage);
           map.put(TAG_DESC_PROMO, PromoDesc);
           map.put(TAG_CATE_PROMO, PromoCate);

           DaftarPromotion.add(map);
           }

           } else {
           }
           } catch (JSONException e) {
           e.printStackTrace();
           }
           return null;
           }

           protected void onPostExecute(String file_url) {
           pDialog.dismiss();
           runOnUiThread(new Runnable() {
           public void run() {

           SetListViewAdapter(DaftarPromotion);
                }
           });
        }
    }
    }

这是将用户重定向到曲线意图的主要活动

package com.javacodegeeks.android.lbs;


import java.text.DecimalFormat;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.javacodegeeks.android.lbs.model.GeoCodeResult;
import com.javacodegeeks.android.lbs.services.GeoCoder;

public class LbsGeocodingActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    private GeoCoder geoCoder = new GeoCoder();


    double longi = 0.0;
    double lati = 0.0;

    String lon = "";
    String lat = "";

    protected LocationManager locationManager;
    protected Location currentLocation;

    protected Button retrieveLocationButton;
    protected Button reverseGeocodingButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
        reverseGeocodingButton = (Button) findViewById(R.id.reverse_geocoding_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

        retrieveLocationButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getLocation();
            }
        });

        reverseGeocodingButton.setOnClickListener(new OnClickListener() {           
            @Override
            public void onClick(View v) {               
                performReverseGeocodingInBackground();
            }
        });

    }    

    protected void performReverseGeocodingInBackground() {
        getLocation();
        new ReverseGeocodeLookupTask().execute((Void[])null);
    }

    protected void getLocation() {

        currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);




    //  if (currentLocation != null) {
//          String message = String.format(
//  "ori"               "Current Location \n Longitude: %1$s \n Latitude: %2$s",
//                  currentLocation.getLongitude(), currentLocation.getLatitude()
//          );
//          Toast.makeText(LbsGeocodingActivity.this, message,
//                  Toast.LENGTH_LONG).show();
    //  }

    }   

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
        //  Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
        //  Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                    //Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
        //  Toast.makeText(LbsGeocodingActivity.this,
        //          "Provider disabled by the user. GPS turned off",
            //      Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            //Toast.makeText(LbsGeocodingActivity.this,
            //      "Provider enabled by the user. GPS turned on",
            //      Toast.LENGTH_LONG).show();
        }

    }


    public class ReverseGeocodeLookupTask extends AsyncTask <Void, Void, GeoCodeResult> {

        private ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            this.progressDialog = ProgressDialog.show(
                    LbsGeocodingActivity.this,
                    "Please wait", // title
                    "Acquiring Location", // message
                    true // indeterminate
            );
        }

        @Override
        protected GeoCodeResult doInBackground(Void... params) {
            return geoCoder.reverseGeoCode(currentLocation.getLatitude(), currentLocation.getLongitude());
        }

        @Override
        protected void onPostExecute(GeoCodeResult result) {
            this.progressDialog.cancel();
            //Toast.makeText(LbsGeocodingActivity.this, result.toString(), Toast.LENGTH_LONG).show();   

            DecimalFormat format = new DecimalFormat();             
            format.setMaximumFractionDigits(4);

             longi = currentLocation.getLongitude();
             lati = currentLocation.getLatitude();

            String lon = format.format(longi);
            String lat = format.format(lati);



            if((lon.equalsIgnoreCase("101.6121")) && (lat.equalsIgnoreCase("3.1581"))   ){
            //Toast.makeText(LbsGeocodingActivity.this, "The curve", Toast.LENGTH_LONG).show();

                 Intent intent = new Intent(LbsGeocodingActivity.this, Curve.class);
                 intent.putExtra("lon",lon);
                 intent.putExtra("lat",lat);
                 startActivity(intent);


            }
            else if((lon.equalsIgnoreCase("101.5178")) && (lat.equalsIgnoreCase("3.0724"))   ){
                //Toast.makeText(LbsGeocodingActivity.this, "Sacc", Toast.LENGTH_LONG).show();

                 Intent intent = new Intent(LbsGeocodingActivity.this, Sacc.class);
                 intent.putExtra("lon",lon);
                 intent.putExtra("lat",lat);
                 startActivity(intent);

                }
            else if((lon.equalsIgnoreCase("101.6356")) && (lat.equalsIgnoreCase("3.1175"))   ){
                //Toast.makeText(LbsGeocodingActivity.this, "Jaya One", Toast.LENGTH_LONG).show();

                 Intent intent = new Intent(LbsGeocodingActivity.this, Jaya.class);
                 intent.putExtra("lon",lon);
                 intent.putExtra("lat",lat);
                 startActivity(intent);

                }

            }

    }

}

2 个答案:

答案 0 :(得分:1)

在日志中:

  

org.json.JSONException:PromoID没有值

表示jsonObject不包含名称为PromoID的任何键。将您的PromoID改为id

public static final String TAG_ID_PROMO = "id";

答案 1 :(得分:0)

我怀疑是那个

list = (ListView) findViewById(R.id.list);
list.setOnItemClickListener(new OnItemClickListener() {
如果找不到R.id.list,

会有麻烦。它是否存在于layout.xml中?如果你说的话会更容易,哪一行是Curve.java:59