这是我的日志猫:
它表示找不到Fatal Exception : Main
和res.resources
,还有很多其他错误。怎么回事?
这是我的主要活动:
public class MainActivity extends Activity {
private final static String highscore = "Your High Score : ";
private ListView listMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListMenu listMenuData[] = new ListMenu[]
{
new ListMenu(R.drawable.repetition, "Repetition\n" + highscore, 0)
};
ListMenuAdapter adapter = new ListMenuAdapter(this, R.layout.listmenu_item, listMenuData);
listMenu = (ListView)findViewById(R.id.listMenu);
View header = (View)getLayoutInflater().inflate(R.layout.listmenu_header, null);
listMenu.addHeaderView(header);
listMenu.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
这是ListMenu,用于setter-getter:
public class ListMenu {
public int icon, highscore;
public String title;
public ListMenu(){
super();
}
public ListMenu(int icon, String title, int highscore){
super();
this.icon = icon;
this.title = title;
this.highscore = highscore;
}
这是我的ListMenuAdapter类:
public class ListMenuAdapter extends ArrayAdapter<ListMenu> {
Context context;
int layoutResourceId;
ListMenu listmenu[] = null;
static class MenuHolder{
ImageView imgMenuIcon;
TextView txtTitle, txtHighScore;
}
public ListMenuAdapter (Context context, int layoutResourceId, ListMenu[] listmenu){
super(context, layoutResourceId, listmenu);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.listmenu = listmenu;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
MenuHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MenuHolder();
holder.imgMenuIcon = (ImageView)row.findViewById(R.id.imgMenuIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
holder.txtHighScore = (TextView)row.findViewById(R.id.txtHighScore);
row.setTag(holder);
}
else holder = (MenuHolder)row.getTag();
ListMenu menu = listmenu[position];
holder.imgMenuIcon.setImageResource(menu.icon);
holder.txtTitle.setText(menu.title);
holder.txtHighScore.setText(menu.highscore);
return row;
}
我遵循并修改了本教程中的代码:click here
有些人说可以通过清理项目/重新启动Eclipse来修复android.content.res.resources$NotFoundException
错误,但它不能为我工作
对不起,很长的帖子,我真的不知道该怎么做 谢谢:D
答案 0 :(得分:2)
如果是这样,那就是问题所在。你应该这样做:
holder.txtHighScore.setText(""+menu.highscore);
由于menu.highscore
为int
,因此会调用方法setText(int resId)
,该方法会加载您在strings.xml
上定义的相关资源ID。
如果你放""+menu.highscore
,它会调用你想要的setText(CharSequence text)
。