0

先生からもらったAndroidチュートリアルの作業を続けていました。このプログラムのアイデアは、レストランの名前、住所、レストランの種類を入力し、これを示すインターフェースを作成することです。

コードを正確にコピーしたと思います。ただし、エラーが発生します。

"The method getType() is undefined for the type Restaurant".

それが何を意味するのか、そしてそれをどのように修正するのか私にはわかりません。

Eclipseが私に提案したのは、「レストランタイプでメソッドgetType()を作成する」でしたが、プログラムを実行し、レストランの詳細を入力して保存すると、nullポインター例外が発生します。

だから私の質問は:

  • エラーはどういう意味ですか?
  • どうすれば修正できますか?

以下は私のメインクラスのLunchlist.javaです。

@SuppressLint({ "ParserError", "ParserError" }) 
public class LunchList extends Activity {
    List<Restaurant> model=new ArrayList<Restaurant>();
    ArrayAdapter<Restaurant> adapter=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lunch_list);

        Button save=(Button)findViewById(R.id.btnSave);

        save.setOnClickListener(onSave);

        ListView list=(ListView)findViewById(R.id.restaurants);

        adapter=new RestaurantAdapter();
        list.setAdapter(adapter);
    }

   private View.OnClickListener onSave=new View.OnClickListener() {
        public void onClick(View v){
            Restaurant r=new Restaurant();
            EditText name=(EditText) findViewById(R.id.name);
            EditText address=(EditText) findViewById(R.id.address);


            r.setName(name.getText().toString());
            r.setAddress(address.getText().toString());

            RadioGroup types=(RadioGroup)findViewById(R.id.types);

            switch (types.getCheckedRadioButtonId()){
            case R.id.sit_down:
                r.setType("sit_down");
                break;
            case R.id.take_out:
                r.setType("take_out");
                break;
            case R.id.delivery:
                r.setType("delivery");
                break;

            }
            adapter.add(r);
        }
        };

        class RestaurantAdapter extends ArrayAdapter<Restaurant> {
            RestaurantAdapter(){
                super(LunchList.this,R.layout.row,model);
            }
        public View getView(int position, View convertView, ViewGroup parent){
            View row=convertView;
            RestaurantHolder holder=null;

            if (row==null){
                LayoutInflater inflater=getLayoutInflater();

                row=inflater.inflate(R.layout.row, parent, false);
                holder=new RestaurantHolder(row);
                row.setTag(holder);
            }
            else{
                holder=(RestaurantHolder)row.getTag();
            }
            holder.populateFrom(model.get(position));
            return(row);
        }
        }

        static class RestaurantHolder {
            private TextView name=null;
            private TextView address=null;
            private ImageView icon=null;
            private View row=null;

            RestaurantHolder(View row){
                this.row=row;

                name=(TextView)row.findViewById(R.id.title);
                address=(TextView)row.findViewById(R.id.address);
                icon=(ImageView)row.findViewById(R.id.icon);
            }

            void populateFrom(Restaurant r){
                name.setText(r.getName());
                address.setText(r.getAddress());

            if (r.getType().equals("sit_down")){
                icon.setImageResource(R.drawable.sitdown);
            }
            else if (r.getType().equals("takeout")){
                icon.setImageResource(R.drawable.takeout);  
            }
            else{
                icon.setImageResource(R.drawable.delivery);
            }

        }
}
}

このクラスはRestaurant.javaです。

public class Restaurant {
private String name="";
private String address="";
private Object type;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public void setType(String string) {
    // TODO Auto-generated method stub

}
public String toString(){
    return(getName());
}






}
4

2 に答える 2

3

これを Restaurant クラスに追加します

public String getType() {
    return type;
}
于 2012-09-19T23:51:43.370 に答える
2

クエストの最初の部分への回答として、提供されたコードに基づいて、クラスにgetType()メソッドを実装する必要があります。Restaurant

に関してはNullPointerException、おそらく実装が初期化されていない可能性getTypeのある値を返すためですtype(実際、setType実装にロジックがないため設定できません)。次のようなことをする必要があります。

public void setType(String string) {
    this.type = string;
}

の使用法で、返されたtype値の null チェックを実行することもできます。使い方次第です。LunchListgetType

于 2012-09-19T23:55:48.517 に答える