Android 尝试调用虚拟方法';int java.util.ArrayList.size()';关于空对象引用
我从服务器得到两种类型的响应第一种是在Android 尝试调用虚拟方法';int java.util.ArrayList.size()';关于空对象引用,android,Android,我从服务器得到两种类型的响应第一种是在路由被发送到服务器时使用JSOn字符串,然后我得到响应{“status”:230,“routes”:[1,9,3]}或者没有被发送,然后我得到{“status”:201}。当前,如果响应为{“status”:201}且我得到以下信息,则应用程序将崩溃: final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route"); int routeListSize =
路由
被发送到服务器时使用JSOn字符串,然后我得到响应{“status”:230,“routes”:[1,9,3]}
或者没有被发送,然后我得到{“status”:201}
。当前,如果响应为{“status”:201}
且我得到以下信息,则应用程序将崩溃:
final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");
int routeListSize = routeList.size(); // Crash
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
if(routes != null){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
else
Log.e("123", "Avoiding null pointer, the routes are null!!!");
}
尝试对空对象引用调用虚拟方法“int java.util.ArrayList.size()”
final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");
int routeListSize = routeList.size(); // Crash
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
if(routes != null){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
else
Log.e("123", "Avoiding null pointer, the routes are null!!!");
}
我怎样才能防止呢?我不想做任何事情,如果服务器响应
Gson gson = new Gson();
StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);
routes = data.getRoutes();
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");
int routeListSize = routeList.size(); // Crash
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
if(routes != null){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
else
Log.e("123", "Avoiding null pointer, the routes are null!!!");
}
在主要活动中:
@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")) {
final ArrayList<Integer> routeList = extras
.getIntegerArrayList("stop_route");
int routeListSize = routeList.size();
if(routeListSize > 0){
}
}
final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");
int routeListSize = routeList.size(); // Crash
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
if(routes != null){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
else
Log.e("123", "Avoiding null pointer, the routes are null!!!");
}
@覆盖
受保护的void onNewIntent(意图){
//TODO自动生成的方法存根
super.onNewIntent(意向);
//getIntent()应始终返回最新的
设定意图(intent);
Bundle extras=getIntent().getExtras();
if(extras!=null&&extras.containsKey(“停止路线”)){
最终阵列列表路由列表=附加
.getIntegerArrayList(“停止路由”);
int routeListSize=routeList.size();
如果(routeListSize>0){
}
}
似乎routeList等于null
final ArrayList<Integer> routeList = extras.getIntegerArrayList("stop_route");
int routeListSize = routeList.size(); // Crash
@Override
protected void onPostExecute(Void result) {
// Intent with Conetxt of the Asyntask class and
if(routes != null){
Intent intent = new Intent(mContext, MainActivity.class);
intent.putIntegerArrayListExtra("stop_route", routes);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
mContext.startActivity(intent);
}
else
Log.e("123", "Avoiding null pointer, the routes are null!!!");
}
final ArrayList routeList=extras.getIntegerArrayList(“stop_route”);
int routeListSize=routeList.size();//崩溃
@凌驾
受保护的void onPostExecute(void结果){
//使用Asyntask类的Conetxt的意图,以及
如果(路由!=null){
意向意向=新意向(mContext,MainActivity.class);
意图.PutIntegerArrayListXTRA(“停止路线”,路线);
intent.addFlags(intent.FLAG\u ACTIVITY\u SINGLE\u TOP);
mContext.startActivity(意图);
}
其他的
Log.e(“123”,“避免空指针,路由为空!!!”;
}
检查附加项是否包含键“stop\u route”
是不够的。您需要检查数组是否为null
。检查的地方是在onPostExecute
中,您不应该调用intent.putIntegerArrayListExtra(“stop\u route”,routes);
如果路由
为空
。