2015-03-08 98 views
0

我做了我的筆記本電腦,從一個活動傳遞信息時,另一延遲誤差意圖putExtra錯誤,NullPointerException異常

第一項活動(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

的可能重複[什麼是空指針異常,以及如何解決?(http://stackoverflow.com /問題/ 218384 /什麼,是一個無效指針異常和如何-DO-I-FIX-IT) – Simon 2015-03-08 17:12:47

回答

0

您還沒有初始化觸點的ArrayList

使用本

private ArrayList<String> contacts =new ArrayList<String>(); 
相關問題