team1name 在小部件类的开头初始化
字符串 team1name = null;
但是当我从返回一个字符串getAcronym()
并分配给 team1name 时,我得到一个空指针异常。我该如何解决这是代码
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
team1name = extras.getString("team1name");
team2name = extras.getString("team2name");
team1score = extras.getString("team1score");
team2score=extras.getString("team2score");
player1=extras.getString("player1");
player2=extras.getString("player2");
extras=null;
team1name=getAcronym(team1name); // Null Pointer Exception
team2name=getAcronym(team2name);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
remoteViews.setTextViewText(R.id.team1name, team1name);
remoteViews.setTextViewText(R.id.team2name,
ComponentName cn = new ComponentName(context, MyWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
}
String getAcronym(String teamName){
if(teamName.equals("Australia")){
teamName="AUS";
}else if(teamName.equals("Pakistan")){
teamName="PAK";
}else if(teamName.equals("India")){
teamName="IND";
}else if(teamName.equals("England")){
teamName="ENG";
}else if(teamName.equals("Sri Lanka")){
teamName="SL";
}else if(teamName.equals("South Africa")){
teamName="RSA";
}
return teamName;
}