Note that there are some explanatory texts on larger screens.

plurals
  1. PORestaurant Program Tutorial
    primarykey
    data
    text
    <p>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. </p> <p>I believe I copied the code exactly. However, I get an error:</p> <pre><code>"The method getType() is undefined for the type Restaurant". </code></pre> <p>I have no idea what that means and how to fix it.</p> <p>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.</p> <p>So my questions are:</p> <ul> <li>What does the error mean?</li> <li>How do I fix it?</li> </ul> <p>Below is my main class Lunchlist.java:</p> <pre><code>@SuppressLint({ "ParserError", "ParserError" }) public class LunchList extends Activity { List&lt;Restaurant&gt; model=new ArrayList&lt;Restaurant&gt;(); ArrayAdapter&lt;Restaurant&gt; 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&lt;Restaurant&gt; { 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); } } } } </code></pre> <p>This class is Restaurant.java:</p> <pre><code>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()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload