我遵循一本名为 Head First Android Development的书的指南 Dawn Griffiths,David Griffiths 的大脑友好指南。 问题是我完全复制了本书建议的代码,但它不起作用。 我已经尝试复制图书网站https://github.com/dogriffiths/HeadFirstAndroid/tree/master/chapter06/Starbuzz中的代码 但它仍然无效
这是代码形式DrinkActivity.java:
package com.hfad.starbuzz;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class DrinkActivity extends Activity {
public static final String EXTRA_DRINKNO = "drinkNo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drink);
}
int drinkNo= (Integer)getIntent().getExtras().get(EXTRA_DRINKNO);
Drink drink = Drink.drinks[drinkNo];
ImageView photo = (ImageView)findViewById(R.id.photo);
photo.setImageResource(drink.getImageResourceID());
photo.setContentDescription(drink.getName());
TextView name=(TextView)findViewById(R.id.name);
name.setText(drink.getName());
TextView description=(TextView)findViewById(R.id.description);
description.setText(drink.getDescription());
}
之间的方法无法解析符号方法
这种方法在下面的另一个类中:
package com.hfad.starbuzz;
public class Drink {
private String name;
private String description;
private int imageResourceId;
public static final Drink[] drinks = {
new Drink("Latte", "A couple of espresso shots with steamed milk" , R.drawable.latte),
new Drink("Cappuccino", "Espresso, hot milk, and a steamed milk foam", R.drawable.cappuccino),
new Drink("Filter", "Highest quality beans roasted and brewed fresh", R.drawable.filter)
};
private Drink(String name, String description, int imageResourceId) {
this.name= name;
this.description= description;
this.imageResourceId= imageResourceId;
}
public String getDescription() {
return description;
}
public String getName() {
return name;
}
public int getImageResourceID() {
return imageResourceId;
}
public String toString() {
return this.name;
}
}
,方法setContentDescription
和setText
应该在我的理解中TextView
和ImageView
。
问题是我已导入TextView
和ImageView
。
那发生了什么?
谢谢
答案 0 :(得分:0)
您的代码在方法之外,将其粘贴到onCreate(
和{
}
)方法中