应用程序不断重启自己

时间:2015-05-17 13:40:28

标签: android

我正在尝试将数据从PostData类的内部类Asynctask-中的doInbackgroud方法传递到MainActivity类的内部类LocationListener中的OnLocationChanged方法。在片刻我应用程序重启自己。我想断言服务器的前5个响应是空的。我该如何解决这个问题?

错误:

05-17 15:07:21.778: I/Process(3224): Sending signal. PID: 3224 SIG: 9
05-17 15:07:26.683: D/Activity(8195): performCreate Call secproduct feature valuefalse
05-17 15:07:26.683: D/Activity(8195): performCreate Call debug elastic valuetrue
05-17 15:07:26.853: V/RenderScript(8195): 0xaf557200 Launching thread(s), CPUs 4
05-17 15:07:26.903: I/Timeline(8195): Timeline: Activity_idle id: android.os.BinderProxy@2a75908a time:366176691
05-17 15:07:26.993: D/AndroidRuntime(8195): Shutting down VM
05-17 15:07:26.993: E/AndroidRuntime(8195): FATAL EXCEPTION: main
05-17 15:07:26.993: E/AndroidRuntime(8195): Process: com.bustracker, PID: 8195
05-17 15:07:26.993: E/AndroidRuntime(8195): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getIntegerArrayList(java.lang.String)' on a null object reference
05-17 15:07:26.993: E/AndroidRuntime(8195):     at com.bustracker.MainActivity$myLocationListener.onLocationChanged(MainActivity.java:249)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.os.Looper.loop(Looper.java:145)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at android.app.ActivityThread.main(ActivityThread.java:5944)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at java.lang.reflect.Method.invoke(Native Method)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at java.lang.reflect.Method.invoke(Method.java:372)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-17 15:07:26.993: E/AndroidRuntime(8195):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

MainActivity:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new myLocationListener(this);
        // location updates: at least 0 meter and 60 seconds change.
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);

    }
      //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        final Context bContext;

        public myLocationListener(Context context){

            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

                PostData sender = new PostData();

                sender.post_data(jSONString, bContext);
                  //receive the Intent.
                Bundle b = getIntent().getExtras();
                b.getIntegerArrayList("stop_route");                
        }
    }
}

PostData类:

public class PostData {
    String jSONString;

    public PostData() {
        super();

    }


    public void post_data(String jSONString, Context context) {
        this.jSONString = jSONString;

    new MyAsyncTask(context).execute(jSONString);
    }
        //Inner class in ht PostData class.
    class MyAsyncTask extends AsyncTask<String, Integer, Void> {
        final Context mContext;

        public MyAsyncTask(Context context){
            mContext = context;
        }

        @Override
        protected Void doInBackground(String... params) {


               ArrayList<Integer> routes = data.getRoutes();

                Intent intent = new Intent(mContext , MainActivity.class);
                intent.putExtra("stop_route" ,routes);
                mContext.startActivity(intent);

            return null;

        }

    }

}

1 个答案:

答案 0 :(得分:0)

  

@覆盖           protected Void doInBackground(String ... params){           }

doInBackground进程在UI主线程中不起作用,而startActivity需要从主线程调用,所以尽量使用 onPostExecute

class MyAsyncTask extends AsyncTask<String, Integer, Void> {
    final Context mContext;
    ArrayList<Integer> routes;

    public MyAsyncTask(Context context){
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
       super.onPreExecute();
       routes = new ArrayList<Integer>();
    }

    @Override
    protected Void doInBackground(String... params) {
        routes = data.getRoutes();
        return null;
    }
    @Override
    protected void onPostExecute(Long result) {
        Intent intent = new Intent(mContext , MainActivity.class);
        intent.putExtra("stop_route" ,routes);
        mContext.startActivity(intent);
    }
}