java - 如何在 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();
}
}
}
解决方案
我认为您的问题是因为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();
}
}
}
推荐阅读
- eclipse - Eclipse EditorPart 保存在 partDeactivated 上
- c# - Recurring Job异步调用WebApi,然后在回调方法中修改Hangfire Job的状态
- javascript - Nuxtjs 结合 spa 预渲染
- c++ - 在 Qt 中显示对话框的密码
- spring-boot - 在 Angular 6 中停止 addEventListener
- sql - 将 JSON 字符串转换为十进制的问题
- postgresql - 在 postgresql 执行语句中使用变量
- spring - 客户端发现后 Eureka Server 停止工作
- java - 在 Spring Boot 应用程序中配置 Jackson mixin
- python - 再次使用密钥列表从列表中创建字典