gpt4 book ai didi

android - 尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.isEmpty()'

转载 作者:行者123 更新时间:2023-11-29 15:50:23 28 4
gpt4 key购买 nike

如果路由已知,我将向服务器发送一次 2 种请求,然后数据将被传输并插入到表中。另一个如果路线未知,则只要有可用数据来计算路线,就会请求服务器向 Android 应用程序提供路线,否则路线为 0。

onPostExecute 中,我正在检查“routeList”是否不为空且不为空,尽管我收到此错误:

错误:

05-25 20:33:29.107: E/AndroidRuntime(1946): FATAL EXCEPTION: main
05-25 20:33:29.107: E/AndroidRuntime(1946): Process: com.bustracker, PID: 1946
05-25 20:33:29.107: E/AndroidRuntime(1946): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
05-25 20:33:29.107: E/AndroidRuntime(1946): at com.bustracker.PostData$MyAsyncTask.onPostExecute(PostData.java:131)
05-25 20:33:29.107: E/AndroidRuntime(1946): at com.bustracker.PostData$MyAsyncTask.onPostExecute(PostData.java:1)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.os.AsyncTask.finish(AsyncTask.java:632)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.os.Handler.dispatchMessage(Handler.java:102)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.os.Looper.loop(Looper.java:145)
05-25 20:33:29.107: E/AndroidRuntime(1946): at android.app.ActivityThread.main(ActivityThread.java:5944)
05-25 20:33:29.107: E/AndroidRuntime(1946): at java.lang.reflect.Method.invoke(Native Method)
05-25 20:33:29.107: E/AndroidRuntime(1946): at java.lang.reflect.Method.invoke(Method.java:372)
05-25 20:33:29.107: E/AndroidRuntime(1946): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-25 20:33:29.107: E/AndroidRuntime(1946): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)

onPostExecute():

ArrayList<Integer> routes = new ArrayList<Integer>();

@Override
protected void onPostExecute(Void result) {
for (int item: routes){
System.out.println("The output from the onPostExecute: "+item);
}
// Intent with Conetxt of the Asyntask class and
if (!routes.isEmpty() && (routes != null)) {
// if(routes !=null ){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.putExtra("stop_distance", distance);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);

} else {
Log.e("123", "Avoiding null pointer, the routes are null!!!");

}

}

onNewIntent():

@Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);

// getIntent() should always return the most recent
setIntent(intent);

Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("stop_route")) {


double distance = extras.getDouble("stop_distance");
System.out.println("The distance is: "+ distance);

if (distance <= 15) {
stop_popup(extras);

} else {
final ArrayList<Integer> routeList = extras
.getIntegerArrayList("stop_route");
route_number = routeList.get(0);
System.out.println("The route number is: " + route_number);
}

} else {
System.out.println("The intent is empty: ");
}

}

最佳答案

你应该首先询问 routes != null 是否像:

// Intent with Conetxt of the Asyntask class and
if (routes != null && !routes.isEmpty()) {
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.putExtra("stop_distance", distance);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);

} else {
Log.e("123", "Avoiding null pointer, the routes are null!!!");

}

&& 评估第一个条件,然后评估第二个条件,因此如果 routes 为 null 并且您执行 routes.IsEmpty() 它将抛出错误。但是,如果您首先询问 routes 是否为空,那么它不会评估第二个条件。

关于android - 尝试在空对象引用上调用虚拟方法 'boolean java.util.ArrayList.isEmpty()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443863/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com