我在制作笔记本时,将信息从一个活动转移到另一个延期错误
第一个活动(listView和按钮) -
public class ContactActivity extends Activity {
private ListView listContact;
private ArrayList<String> contacts;
private Intent intentContant;
private boolean sendText;
private Bundle bundle;
private String getContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
bundle = getIntent().getBundleExtra("android.intent.extra.INTENT");
sendText = bundle.getBoolean("SetNewText");
if(sendText){
Intent intent = getIntent();
getContent = intent.getStringExtra("FromAddEditString");
contacts.add(getContent);
}
listContact = (ListView) findViewById(R.id.list_contact);
intentContant = new Intent(ContactActivity.this, AddEditActivity.class);
contacts = new ArrayList<String>();
ArrayAdapter<String> contAdapter = new ArrayAdapter<String>(ContactActivity.this,
R.layout.list_item,
contacts);
listContact.setAdapter(contAdapter);
listContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
intentContant.putExtra("ContactName", parent.getAdapter().getItem(position).toString());
bundle = new Bundle();
bundle.putBoolean("ImportContact", true);
intentContant.putExtra("android.intent.extra.INTENT", bundle);
startActivity(intentContant);
}
});
}
public void onClick(View v){
switch(v.getId()){
case R.id.btnAdd:
intentContant.putExtra("ContactName", "");
bundle = new Bundle();
bundle.putBoolean("ImportContact", false);
intentContant.putExtra("android.intent.extra.INTENT", bundle);
startActivity(intentContant);
break;
case R.id.btnBack:
intentContant = new Intent(ContactActivity.this, MainActivity.class);
startActivity(intentContant);
break;
}
}
}
第二项活动(编辑/添加联系人)
public class AddEditActivity extends Activity {
private String result;
private Boolean resultBoolean;
private EditText editText;
private Bundle extras;
private Bundle extrasBoolean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit);
extras = getIntent().getExtras();
extrasBoolean = getIntent().getBundleExtra("android.intent.extra.INTENT");
resultBoolean = extrasBoolean.getBoolean("ImportContact");
editText = (EditText) findViewById(R.id.etAddEdit);
if(resultBoolean) {
result = extras.getString("ContactName");
editText.setText(result);
} else if(!resultBoolean)
editText.setText("");
}
public void add_edit_click(View v){
Bundle btnAccept = new Bundle();
Intent intentAddEdit1 = new Intent(AddEditActivity.this, ContactActivity.class);
Intent intentAddEdit2 = new Intent(AddEditActivity.this, ContactActivity.class);
switch (v.getId()){
case R.id.btnAccept:
if (editText.getText().length() == 0)
Toast.makeText(getApplicationContext(), "Введите текст", Toast.LENGTH_SHORT).show();
else {
btnAccept.putBoolean("SetNewText", true);
intentAddEdit1.putExtra("android.intent.extra.INTENT", btnAccept);
String result = editText.getText().toString();
intentAddEdit2.putExtra("FromAddEditString", result);
startActivity(intentAddEdit1);
}
break;
case R.id.btnCancel:
btnAccept.putBoolean("SetNewText", false);
intentAddEdit1.putExtra("android.intent.extra.INTENT", btnAccept);
startActivity(intentAddEdit1);
break;
}
}
}
LogCat -
引起:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.itschoolsamsung.myapp.ContactActivity.onCreate(ContactActivity.java:36)
我做错了什么?
答案 0 :(得分:0)
您尚未初始化联系人arraylist
使用此
private ArrayList<String> contacts =new ArrayList<String>();