Android上的startActivity(intent)无法正常工作

时间:2014-03-21 04:20:23

标签: java android android-intent

所以我试图通过序列化传递一些信息,但由于某些原因它不起作用,我写了几个Log.e()进行测试,根据LogCat它说它不是通过了startactivity(intent)

这是我的代码

public class MainActivity extends Activity {

    FileManager file = new FileManager();
    FileInputStream fileInput; 
    FileOutputStream fileOutput; 
    SortedArrayList<Contact> contactList = new SortedArrayList<Contact>();
    ArrayList<Contact> theList =new ArrayList<Contact>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            fileInput = openFileInput("contacts.txt");
            Log.e("FILE INPUT","FOUND!!");
            contactList = file.read(fileInput,getApplicationContext());
            Log.e("_____",contactList.toString());
        } 
        catch (Exception e) {
            Log.e("Error","ERROR CREATING FILE");
        }

        if(contactList.size() != 0){
            ListView lv =(ListView)findViewById(R.id.contactList);
            for(int i=0;i<contactList.size();i++){
                theList.add(contactList.get(i));
            }
            ArrayAdapter<Contact> adapter = new MyListAdapter();
            lv.setAdapter(adapter);
            clickOnContact();
        }

        // Create The Adapter with passing ArrayList as 3rd parameter
        //OrderAdapter arrayAdapter =  new OrderAdapter(this, R.layout.list_view, contactListNameView, contactListLastNameView, contactListCellphoneView);    
        //Set The Adapter
        //lv.setAdapter(arrayAdapter); 

    }

    private void clickOnContact() {
        ListView list = (ListView) findViewById(R.id.contactList);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int pos,
                    long id) {  
            Contact Contact = theList.get(pos);
            Log.e("contact: ",Contact.getFirstName());
            try{    
                Intent intent = new Intent(MainActivity.this, ShowContactActivity.class);
                Log.e("pass ","1");
                intent.putExtra("Contact",Contact);
                Log.e("pass ","2");
                startActivity(intent);
            }
            catch (Exception e){
                Log.e("didnt","pass catch");
            }   
        }       
    }); 
}

private class MyListAdapter extends ArrayAdapter<Contact> {

    public MyListAdapter() {
        super(MainActivity.this, R.layout.list_view, theList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;    
        if(itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.list_view, parent, false);
        }   
        Contact contact = theList.get(position);    
        //Fill First Name
        TextView name = (TextView) itemView.findViewById(R.id.contactName);
        name.setText(contact.getFirstName());   
        //Fill Last Name
        TextView lastname = (TextView) itemView.findViewById(R.id.contactLastName);
        lastname.setText(contact.getLastName());
        return itemView;
    }

}

这是Activity试图联系:

public class ShowContactActivity extends Activity {

    TextView name;
    TextView lastname;
    TextView email;
    TextView cell;
    TextView homenumber;
    Contact contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_contact_info);
        // Show the Up button in the action bar.
        setupActionBar();
        name = (TextView) findViewById(R.id.nametextview);
        lastname = (TextView) findViewById(R.id.lastnametextview);
        email = (TextView) findViewById(R.id.emailtextview);
        cell = (TextView) findViewById(R.id.celltextview);
        homenumber = (TextView) findViewById(R.id.homenumbertextview);
        Contact contact = new Contact();

        try{
        Intent i = getIntent();
        contact = (Contact) i.getSerializableExtra("Contact");
        }
        catch (Exception e){
            Log.e("problema","con serializacion");
        }
        name.setText(contact.getFirstName());
        lastname.setText(contact.getLastName());
        cell.setText(contact.getCell());
        homenumber.setText(contact.getHomeNumber());
        email.setText(contact.getEmail());


    }

}

1 个答案:

答案 0 :(得分:2)

使用

Contact contact = theList.get(pos);  //contact is the object of the class (small c)