Reputation: 157
I have the following scenario in a simple app: 1- the user clicks the menu button on the MainActivity to open another activity on a whole new screen: addTimeActivity, where they can enter a note and its time. 2- The new activity has two edit texts: the time and note, the user enters the data and then clicks save, or they can click the cancel button when they don't want to save the note. 3- the addTimeActiviy passes the data through an intent. 4- the addTimeActivity finishes. 5- The data is supposed to be saved in an adapter which in turn displays it as a list on the MainActivity, the method addTimeRecord in The TimeTrackerAdapter is adding the String data to the times ArrayList. Now when the save button is clicked, a NullPointerException appears.
Here's the MainActivity:
public class MainActivity extends ActionBarActivity {
ListView listView;
TimeTrackerAdapter timeTrackerAdapter;
public static final int TIME_ENTRY_REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeTrackerAdapter=new TimeTrackerAdapter();
listView=(ListView) findViewById(R.id.recordList);
listView.setAdapter(timeTrackerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.menu_list_item, menu);
return true;
}
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
if(requestCode==TIME_ENTRY_REQUEST_CODE)
{
if(resultCode==RESULT_OK)
{
Bundle extras=getIntent().getExtras();
String time=extras.getString("time");
String notes=extras.getString("notes");
timeTrackerAdapter.addTimeRecord(time, notes);
timeTrackerAdapter.notifyDataSetChanged();
}
}
}
@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.
if(item.getItemId()==R.id.add_time_menu_item)
{
Intent intent=new Intent(getApplicationContext(),AddTimeActivity.class);
startActivityForResult(intent,TIME_ENTRY_REQUEST_CODE);
return true;
}
return super.onOptionsItemSelected(item);
} }
and here's the addTimeActivity:
public class AddTimeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_time);
}
public void onCancel(View view)
{
finish();
}
public void onSave(View view)
{
Intent intent = new Intent(AddTimeActivity.this,MainActivity.class);
EditText editTextNotes=(EditText) findViewById(R.id.notes_edit_text);
intent.putExtra("notes",editTextNotes.getText().toString());
EditText editTextTime=(EditText) findViewById(R.id.time_edit_text);
intent.putExtra("time", editTextTime.getText().toString());
this.setResult(RESULT_OK,intent);
startActivity(intent);
finish();
}
}
The adapter:
public class TimeTrackerAdapter extends BaseAdapter{
ArrayList<TimeRecord> times=new ArrayList<TimeRecord>();
TextView timeView,noteView;
@Override
public int getCount() {
// TODO Auto-generated method stub
return times.size();
}
public TimeTrackerAdapter()
{
}
@Override
public Object getItem(int index) {
// TODO Auto-generated method stub
return getItem(index);
}
@Override
public long getItemId(int index) {
// TODO Auto-generated method stub
return index;
}
public void addTimeRecord(String time,String notes)
{
TimeRecord timeRecord=new TimeRecord(time,notes);
times.add(timeRecord);
}
@Override
public View getView(int index, View view, ViewGroup parent) {
// TODO Auto-generated method stub
if(view==null)
{
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
view=inflater.inflate(R.layout.time_tracker, parent,false);
}
TimeRecord time=times.get(index);
TextView timeText=(TextView) view.findViewById(R.id.timeText);
timeText.setText(time.getTime());
TextView noteText=(TextView) view.findViewById(R.id.noteText);
noteText.setText(time.getNote());
return view;
}
}
Upvotes: 0
Views: 78
Reputation: 1055
you are not initializing your member variable timeTrackerAdapter
! in your onCreate you do this TimeTrackerAdapter timeTrackerAdapter=new TimeTrackerAdapter();
but should be timeTrackerAdapter=new TimeTrackerAdapter();
also you are using the wrong intent in your onActivityResult. it should be like this:
protected void onActivityResult(int requestCode,int resultCode,Intent data) // Intent data is the intent that includes the data of your AddTimeActivity
{
if(requestCode==TIME_ENTRY_REQUEST_CODE)
{
if(resultCode==RESULT_OK)
{
Bundle extras=data.getExtras(); // not getIntent().getExtras()
String time=extras.getString("time");
String notes=extras.getString("notes");
timeTrackerAdapter.addTimeRecord(time, notes);
timeTrackerAdapter.notifyDataSetChanged();
}
}
}
Upvotes: 1