我在一个简单的应用程序中有以下情形:
1-用户单击MainActivity上的菜单按钮以在整个新屏幕上打开另一个活动:addTimeActivity,他们可以在其中输入注释及其时间。
2-新活动具有两个编辑文本:时间和便笺,用户输入数据然后单击保存,或者在不想保存便笺时可以单击取消按钮。
3- addTimeActiviy通过意图传递数据。
4-addTimeActivity完成。
5-应该将数据保存在适配器中,该适配器又将其显示为MainActivity上的列表,TimeTrackerAdapter中的addTimeRecord方法将String数据添加到时间ArrayList中。
现在,单击“保存”按钮时,将出现NullPointerException。
这是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);
} }
这是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();
}
}
适配器:
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;
}
}
最佳答案
您没有初始化成员变量timeTrackerAdapter
!在您的onCreate中执行此操作,但应为TimeTrackerAdapter timeTrackerAdapter=new TimeTrackerAdapter();
同样,您在onActivityResult中使用了错误的意图。应该是这样的:
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();
}
}
}