首页 > 解决方案 > 如何在 Android 中从 Intent 获取数据

问题描述

我需要从意图中获取数据。它在 onCreate 方法中有效,但在位于 onCreate 方法下方的 Home 类中无效。我收到此错误:任何人都可以帮助我,我该如何解决这个问题?解决方法:voznja_id 在 oncreate 方法中有效,但在名为 Home 的异步任务类中无效。谢谢

**Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter value**
       @Override
    protected void onCreate(Bundle savedInstanceState) {
     Bundle extras = getIntent().getExtras();
            username = extras.getString("username");
            password = extras.getString("password");
            Intent intent = getIntent();
            voznja = (Voznja) intent.getSerializableExtra("voznja");
            System.out.println(voznja.getId())//ID = 118 it works
}
 public class Home extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... strings) {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = new FormBody.Builder()
                    .add("voznja_id", voznja.getId())// IT'S NULL HERE
                    .build();
            Request request = new Request.Builder()
                    .url("http://www.autotrack.rs/android_juzna_backa/get_voznja.php?"
                    .method("POST", body)
                    .build();
            try {
                response = client.newCall(request).execute();
//                System.out.println(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "";
        }


        protected void onPostExecute(String unused) {
            try {
                System.out.println(voznja.getId());
                String rezultat = response.body().string();
                //System.out.println(rezultat);
                Gson gson = new Gson();
                Type type = new TypeToken<Data>() {
                }.getType();
                Data data = gson.fromJson(rezultat, type);
                // System.out.println(model);//
                //listaVoznjiAL.add(data.data);
                //adapter.notifyDataSetChanged();

                Intent i = new Intent(Unos.this, Landing.class);
                i.putExtra("data",data);
                i.putExtra("voznja", voznja);
                i.putExtra("username", username);
                i.putExtra("password", password);
                startActivity(i);
            } catch (Exception m) {
                m.printStackTrace();
            }

        }
    }

标签: javaandroid

解决方案


我认为您的问题是因为Home是类,并且当对象尚未初始化ASyncTask时它在后台运行。你可以通过类构造函数voznja的参数传递voznja的id,如下所示:Home

public class Home extends AsyncTask<String, String, String> {
        private voznjaId;
        public Home(int voznjaId){
            this.voznjaId = voznjaId;
        }

        @Override
        protected String doInBackground(String... strings) {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("text/plain");
            RequestBody body = new FormBody.Builder()
                    .add("voznja_id", voznjaId)
                    .build();
            Request request = new Request.Builder()
                    .url("http://www.autotrack.rs/android_juzna_backa/get_voznja.php?"
                    .method("POST", body)
                    .build();
            try {
                response = client.newCall(request).execute();
//                System.out.println(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }

            return "";
        }


        protected void onPostExecute(String unused) {
            try {
                System.out.println(voznja.getId());
                String rezultat = response.body().string();
                //System.out.println(rezultat);
                Gson gson = new Gson();
                Type type = new TypeToken<Data>() {
                }.getType();
                Data data = gson.fromJson(rezultat, type);
                // System.out.println(model);//
                //listaVoznjiAL.add(data.data);
                //adapter.notifyDataSetChanged();

                Intent i = new Intent(Unos.this, Landing.class);
                i.putExtra("data",data);
                i.putExtra("voznja", voznja);
                i.putExtra("username", username);
                i.putExtra("password", password);
                startActivity(i);
            } catch (Exception m) {
                m.printStackTrace();
            }

        }
    }

推荐阅读