Reputation: 287
I am getting a nullpointer exception when i want to add an object to my list/database. I am inflating the object's xml file, yet it cannot find the TextView. The exception occurs in txtView.setText();
My MainActivity:
public class MainActivity extends ActionBarActivity {
protected PhoneDBHelper db;
List<Phone> list;
MyAdapter adapt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new PhoneDBHelper(this);
list = db.getAllPhones();
adapt = new MyAdapter(this, R.layout.list_inner_view, list);
ListView listPhone = (ListView)findViewById(R.id.listView1);
listPhone.setAdapter(adapt);
}
public void addPhoneNow(View v){
EditText t = (EditText) findViewById(R.id.editText1);
EditText author = (EditText) findViewById(R.id.editText);
String s = t.getText().toString();
String a = author.getText().toString();
if(s.equalsIgnoreCase("")){
Toast.makeText(this, "Enter the phone name first!", Toast.LENGTH_LONG);
} else {
Phone phone = new Phone(s, a);
db.addPhone(phone);
Log.d("phones", "data added");
t.setText("");
author.setText("");
adapt.add(phone);
adapt.notifyDataSetChanged();
}
}
@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);
}
private class MyAdapter extends ArrayAdapter<Phone>{
Context context;
List<Phone> phoneList = new ArrayList<Phone>();
int layoutResourceId;
public MyAdapter(Context context, int layoutResourceId, List<Phone> phoneList){
super(context, layoutResourceId, phoneList);
this.layoutResourceId = layoutResourceId;
this.phoneList = phoneList;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_inner_view, parent, false);
TextView textView = (TextView) convertView.findViewById(R.id.textBookView);
Phone current = phoneList.get(position);
textView.setText(current.getPhoneName() + " - " + current.getAuthor());
return rowView;
}
}
My XML file for the object:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textBookView"
android:focusable="false"
android:focusableInTouchMode="false"/>
Am i overlooking something? Sorry if this question has already been asked, but I can't find my solution.
Upvotes: 1
Views: 386
Reputation: 8519
Change you getView method to this:
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_inner_view, parent, false);
}
TextView textView = (TextView) convertView.findViewById(R.id.textBookView);
Phone current = phoneList.get(position);
textView.setText(current.getPhoneName() + " - " + current.getAuthor());
return convertView;
}
convertView is the old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view. Heterogeneous lists can specify their number of view types, so that this View is always of the right type (see getViewTypeCount() and getItemViewType(int)). I got this from here http://developer.android.com/reference/android/widget/Adapter.html
Upvotes: 1