将Intent从另一个Activity传递给mainActivity

时间:2015-05-17 12:24:23

标签: android

我正在尝试将数据从PostData类的内部类Asynctask-中的doInbackgroud方法传递到MainActivity类的内部类LocationListener中的OnLocationChanged方法。在片刻,应用程序不断重启,我在logcat中收到以下错误。

路线varibale具有这些值[7,31]。我无法理解我正在检查extra是否为空,但是输入了if语句。

该应用程序正常工作没有意图(添加的代码,我考虑//Like This//

我希望有人可以帮助我,因为我在8小时后就遇到了这个问题。

我感谢任何帮助。

错误:

05-17 18:42:44.391: E/AndroidRuntime(18739): FATAL EXCEPTION: main
05-17 18:42:44.391: E/AndroidRuntime(18739): Process: com.bustracker, PID: 18739
05-17 18:42:44.391: E/AndroidRuntime(18739): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
05-17 18:42:44.391: E/AndroidRuntime(18739):    at com.bustracker.MainActivity$myLocationListener.onLocationChanged(MainActivity.java:242)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.os.Looper.loop(Looper.java:145)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at android.app.ActivityThread.main(ActivityThread.java:5944)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at java.lang.reflect.Method.invoke(Native Method)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at java.lang.reflect.Method.invoke(Method.java:372)
05-17 18:42:44.391: E/AndroidRuntime(18739):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-17 18:42:44.391: E/AndroidRuntime(18739):    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);
       //I added "this" here//.
        LocationListener ll = new myLocationListener(this);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);

    }
      //Inner class in MainActivity
    class myLocationListener implements LocationListener {
        // I added here the bContext and the constructor//
        final Context bContext;

        public myLocationListener(Context context){
            bContext = context;
        }

        @Override
        public void onLocationChanged(Location location) {

                PostData sender = new PostData();
               // I added here the context  parameter.//
                sender.post_data(jSONString, bContext);
                  //I added here this part to receive the intent from onPostExecute //
                Bundle extra = getIntent().getExtras();
                if (extras != null) {
                    ArrayList<Integer> c = extras
                            .getIntegerArrayList("stop_route");
                    for (int item : c) {
                        System.out.println("The Intent is not empty: "
                                + item);
                    }
                }               
        }

PostData类:

    public class PostData {
        String jSONString;
//I added the route class variable//
       ArrayList<Integer> routes;

        public PostData() {
            super();

        }

       //I added the context parameter//
        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> {

           //I added the constructor//
            final Context mContext;

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

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


                  routes = data.getRoutes();


                return null;

            }
           //I added this method.
                @Override
        protected void onPostExecute(Void result) {
            // Intent with Conetxt of the Asyntask class and
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("stop_route", routes);
            mContext.startActivity(intent);

        }

        }

    }

1 个答案:

答案 0 :(得分:0)

将您的代码更改为以下

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, Void, ArrayList<Integer>> {
    final Context mContext;

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

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


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



        return routes;

    }

}

   @Override
    protected void onPostExecute(ArrayList<int> result) {
        super.onPostExecute(result);
         Intent intent = new Intent(mContext , MainActivity.class);
            intent.putExtra("stop_route" ,result);
            mContext.startActivity(intent);
    }

}