XML : How do I make a custom adapter work for a class in android?

Basically I'm just trying to make a list with two textviews (a string and an int) with the string coming from an editText, and one button. I created a custom adapter and also an xml file for the list item. I've been stuck on this problem for a few days. This is my first android thing ever and also a refresher on java, so any advice helps! When I run it the app crashes and the compiler is saying (These are the important things I pulled out)

  java.lang.IllegalStateException: Could not execute method of the activity    Caused by: java.lang.reflect.InvocationTargetException  at java.lang.reflect.Method.invoke(Native Method)                               at java.lang.reflect.Method.invoke(Method.java:372)    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method   boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference at com.example.avengerlol.onmyown.MainActivity.onAddItem(MainActivity.java:63)    

Here's what I have so far:

My MainActivity

  public class MainActivity extends Activity {  public ArrayList<SuperMan> list;  public class SuperMan {      public int counter = 0;      public String theName;  }    @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);        ArrayList<SuperMan> list = new ArrayList<SuperMan>();        MyCustomAdapter adapter = new MyCustomAdapter(list, this);        ListView lView = (ListView)findViewById(R.id.the_whole_list);      lView.setAdapter(adapter);  }    @Override  public boolean onCreateOptionsMenu(Menu menu) {      // Inflate the menu; this adds items to the action bar if it is present.      getMenuInflater().inflate(R.menu.menu_main, menu);      return true;  }    @Override  public boolean onOptionsItemSelected(MenuItem item) {      // Handle action bar item clicks here. The action bar will      // automatically handle clicks on the Home/Up button, so long      // as you specify a parent activity in AndroidManifest.xml.      int id = item.getItemId();        //noinspection SimplifiableIfStatement      if (id == R.id.action_settings) {          return true;      }        return super.onOptionsItemSelected(item);  }      public void onAddItem (View v)  {      EditText etNewItem = (EditText) findViewById(R.id.addItemText);      SuperMan letsGo = new SuperMan();      letsGo.theName = etNewItem.getText().toString();        list.add(letsGo);      etNewItem.setText("");    }  }    

My custom adapter class

  public class MyCustomAdapter extends BaseAdapter implements ListAdapter {  private ArrayList<MainActivity.SuperMan> list = new ArrayList<>();  private Context context;    public MyCustomAdapter(ArrayList<MainActivity.SuperMan> list, Context context) {      this.list = list;      this.context = context;  }    @Override  public int getCount() {      return list.size();  }    @Override  public Object getItem(int pos) {      return list.get(pos);  }    @Override  public long getItemId(int pos) {      return 0;    }    @Override  public View getView(final int position, View convertView, ViewGroup parent) {      View view = convertView;      if (view == null) {          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          view = inflater.inflate(R.layout.superman_list, null);      }        //Handle TextView and display string from your list        TextView listItemText = (TextView) view.findViewById(R.id.itemName);      TextView listItemCounter = (TextView) view.findViewById(R.id.itemCounter);      Button increaseButton = (Button) view.findViewById(R.id.increaseButton);          return view;  }      }    

My main xml

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"                                                  android:layout_height="match_parent"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   android:paddingBottom="@dimen/activity_vertical_margin"       tools:context=".MainActivity">  <ListView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_above="@+id/addItemText"      android:id="@+id/the_whole_list"      />        <EditText      android:id="@+id/addItemText"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentBottom="true"      android:hint="Enter a new item"      android:layout_alignParentLeft="true"      android:layout_alignParentStart="true"      android:layout_toLeftOf="@+id/btnAddItem"      android:layout_toStartOf="@+id/btnAddItem"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Add Item"      android:id="@+id/btnAddItem"      android:layout_alignParentBottom="true"      android:layout_alignParentRight="true"      android:layout_alignParentEnd="true"      android:onClick="onAddItem"/>    </RelativeLayout>    

My list item xml

  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="horizontal" android:layout_width="match_parent"  android:layout_height="match_parent">    <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/itemName"        />    <TextView      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/itemCounter"        />    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="+"      android:id="@+id/increaseButton"      />    </LinearLayout>    

No comments:

Post a Comment