2017-04-11 95 views
1

我下面一本書叫頭第一個Android開發 腦友好的嚮導到黎明格里菲斯,戴維·格里菲思的方針。 問題是,我完全複製了本書建議的代碼,但它不起作用。 我已經嘗試代碼的圖書網站複製https://github.com/dogriffiths/HeadFirstAndroid/tree/master/chapter06/Starbuzz 但它仍然無法正常工作的Android無法解析符號進口

這是代碼的形式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; 
} 
} 

和方法setContentDescriptionsetText應該在TextViewImageView我的理解。

問題是我已經輸入了TextViewImageView

那麼發生了什麼?

謝謝

回答

0

您的代碼的方法之外,將其粘貼到{}

+0

之間的onCreate()方法太感謝你了,這是一個明顯的錯誤,但像我這樣的起動機無法找到它,我會記住它。謝謝。 –

+0

你能接受答案嗎? –