餐厅计划教程 [英] Restaurant Program Tutorial

查看:147
本文介绍了餐厅计划教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid教程,我的老师给我继续工作。该方案的想法是在餐厅的餐厅的名称,地址和类型输入,并创建,显示这样的一个接口。

我相信我正好复制了code。不过,我得到一个错误:

 的方法GetType()是未定义的类型餐厅。

我不知道这意味着什么,以及如何解决它。

该建议的Eclipse给了我是创建类型餐厅方法GetType(),但是当我这样做,我跑我的程序时,进入我的餐厅的详细信息,并保存他们得到一个空指针异常。

所以我的问题是:


  • 是什么错误呢?

  • 如何解决这个问题?

下面是我的主类Lunchlist.java:

  @燮pressLint({ParserError,ParserError})
公共类LunchList延伸活动{
    清单<餐厅>模型=新的ArrayList<餐厅>();
    ArrayAdapter<餐厅>适配器= NULL;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_lunch_list);        按钮保存=(按钮)findViewById(R.id.btnSave);        save.setOnClickListener(的onSave);        ListView控件列表=(ListView控件)findViewById(R.id.restaurants);        适配器=新RestaurantAdapter();
        list.setAdapter(适配器);
    }   私人View.OnClickListener的onSave =新View.OnClickListener(){
        公共无效的onClick(视图v){
            餐厅R =新餐厅();
            的EditText名=(的EditText)findViewById(R.id.name);
            的EditText地址=(EditText上)findViewById(R.id.address);
            r.setName(name.getText()的toString());
            r.setAddress(address.getText()的toString());            RadioGroup中类型=(RadioGroup中)findViewById(R.id.types);            开关(types.getCheckedRadioButtonId()){
            案例R.id.sit_down:
                r.setType(sit_down);
                打破;
            案例R.id.take_out:
                r.setType(take_out);
                打破;
            案例R.id.delivery:
                r.setType(交付);
                打破;            }
            adapter.add(R);
        }
        };        类RestaurantAdapter扩展ArrayAdapter<餐厅> {
            RestaurantAdapter(){
                超(LunchList.this,R.layout.row,模型);
            }
        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            查看排= convertView;
            RestaurantHolder支架=无效;            如果(行== NULL){
                LayoutInflater吹气= getLayoutInflater();                行= inflater.inflate(R.layout.row,父母,假);
                持有人=新RestaurantHolder(行);
                row.setTag(保持器);
            }
            其他{
                支架=(RestaurantHolder)row.getTag();
            }
            holder.populateFrom(model.get(位置));
            返回(行);
        }
        }        静态类RestaurantHolder {
            私人TextView的名字= NULL;
            私人TextView的地址= NULL;
            私人ImageView的图标= NULL;
            私人视图行= NULL;            RestaurantHolder(查看行){
                this.row =排;                名称=(TextView中)row.findViewById(R.id.title);
                地址=(TextView中)row.findViewById(R.id.address);
                图标=(ImageView的)row.findViewById(R.id.icon);
            }            无效populateFrom(餐厅R){
                name.setText(r.getName());
                address.setText(r.getAddress());            如果(r.getType()。等于(sit_down)){
                icon.setImageResource(R.drawable.sitdown);
            }
            否则,如果(r.getType()。等于(外卖)){
                icon.setImageResource(R.drawable.takeout);
            }
            其他{
                icon.setImageResource(R.drawable.delivery);
            }        }
}
}

本类Restaurant.java:

 公共类餐厅{
私人字符串名称=;
私人字符串的地址=;
私有对象类型;公共字符串的getName(){
    返回名称;
}
公共无效setname可以(字符串名称){
    this.name =名称;
}
公共字符串的getAddress(){
    退货地址;
}
公共无效setAddress(字符串地址){
    this.address =地址;
}
公共无效的setType(字符串字符串){
    // TODO自动生成方法存根}
公共字符串的toString(){
    返回(的getName());
}
}


解决方案

添加到您的餐厅类

 公共字符串的getType(){
    返回类型;
}

I was continuing work on my Android tutorial that my teacher gave to me. The idea of the program is to enter in a restaurant name, address and type of restaurant and create an interface that shows this.

I believe I copied the code exactly. However, I get an error:

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

I have no idea what that means and how to fix it.

The suggestion Eclipse gave me was to "Create method getType() in type restaurant" but when I do it I get a null pointer exception when running my program, entering the details of my restaurant and saving them.

So my questions are:

  • What does the error mean?
  • How do I fix it?

Below is my main class 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);
            }

        }
}
}

This class is 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());
}






}

解决方案

Add this to your Restaurant class

public String getType() {
    return type;
}

这篇关于餐厅计划教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆