使用自定义视图将String数组传递给另一个Activity

时间:2017-01-11 17:45:03

标签: java android eclipse listview

我在将数组传递到listview时遇到问题 我可以将图像传递给第二个活动但是当我 尝试将数组传递给listview我得到一个lang.NullPointerException错误 和应用程序崩溃。

任何有用的输入都会很棒谢谢

这是代码 主要活动

Pin to Start

第二个活动我想将数组传递给

package com.example.androidlistview;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;



public class MainActivity extends Activity {



    ListView listView;
    // Defined the text values to show in ListView
    String[] values = new String[]{"Barack Obama","Donald Trump","Bill Clinton",
            "Hillary Clinton","Joe Biden"};
    // Defined the image ids to show in ListView
    Integer[] images = {
            R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,
            R.drawable.image5   
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // create the custom view
        CustomView adapter = new CustomView(MainActivity.this, values, images);

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.container);
        //set adapter
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {


                if(position == 0){

                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details= new String[]{"Barack Obama","The White House","1-234-567-8900",
                            "Barack.Obama@whitehouse.com","note"};
                    Integer image = images[0];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);   
                }
                if(position == 1){
                    Intent contact = new Intent(MainActivity.this, Details.class); 
                    String[] details= new String[] {"Donald Trump","Trump Tower","1-234-567-8901",
                            "Donald.Trump@whitehouse.com","note"};
                    Integer image = images[1];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);
                }
                 if(position == 2){
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details = new String[] {"Bill Clinton","New York","1-234-567-8902",
                            "Bill.Clinton@whitehouse.com","note"};
                    Integer image = images[2];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);                 
                 }
                 if(position == 3){
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details = new String[] {"Hillary Clinton","New Jersey","1-234-567-8903",
                            "Hillary.Clinton@whitehouse.com","note"};
                    Integer image = images[3];
                    contact.putExtra("image", image); 
                    contact.putExtra("details", details);
                    startActivity(contact);

                 }
                 if(position == 4){
                    Intent contact = new Intent(MainActivity.this, Details.class);
                    String[] details= new String[] {"Joe Biden","OHIO","1-234-567-8903",
                            "Joe.Biden@whitehouse.com","note"};
                    Integer image = images[4];
                    contact.putExtra("image", image);
                    contact.putExtra("details", details);
                    startActivity(contact);
                }
            }
        });
    }
}

以及我用于第二个活动详细信息的自定义视图

package com.example.androidlistview;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Details extends Activity {

    ListView list;

    String []web={"Name:","Address:","Phone Number:","Email:","Note:"};
    int image;
    String[] details;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        // create the custom view
        CustomList Adapter= new CustomList(Details.this, web, details);//, details should go here
        // Get ListView object from xml
        list=(ListView)findViewById(R.id.list);
        //set adapter
        list.setAdapter(Adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener(){


        @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
            }

        });

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {       
            image= bundle.getInt("image");
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);    
            imageView.setImageResource(image);

        }

    }
}

1 个答案:

答案 0 :(得分:0)

这就是我为解决这个问题而采取的措施 主要活动

package com.example.androidcontacts;



import android.app.ListActivity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {

    ListView mylist;
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Uri allContacts = Uri.parse("content://"+"com.example.androidcontacts.Books" + "/Contact");

        Cursor c;
        CursorLoader cursorLoader = new CursorLoader(
        this,allContacts,null,null,null,null);
        c = cursorLoader.loadInBackground();

        String[] columns = new String[] {Contact._ID,Contact.TITLE,Contact.AUTHOR,Contact.YEAR,Contact.IMAGE};

        int[] views = new int[] {R.id.contactID,R.id.contactName,R.id.contactAuthor,R.id.contactYear,R.id.Image};

        SimpleCursorAdapter adapter;
        adapter = new SimpleCursorAdapter(this, R.layout.activity_main, 
                c, columns, views,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        this.setListAdapter(adapter); 
        }





     @Override
     protected void onListItemClick(ListView list, View view, int position, long id) {

         //Intent i=new Intent(MainActivity.this,Details.class);
         //startActivity(i);
 }

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
              return true;
     }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        int id = item.getItemId();

        if(id == R.id.action_search){
            Intent intent = new Intent(this, Search.class);
            startActivity(intent);
            finish();
            return true;
        }

        if(id == R.id.action_add){
            Intent intent = new Intent(this, Add.class);
            startActivity(intent);
            finish();
            return true;
        }


        return super.onOptionsItemSelected(item);
    }



}

第二项活动详情

package com.example.androidlistview;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Details extends Activity {


    //Variables for Bundle
    int image;
    String name;
    String address;
    String phone;
    String email;
    String note;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Button SMS = (Button) findViewById(R.id.button1);
        SMS.setOnClickListener(new OnClickListener() {
            //when button1 is clicked open messenger
            @Override
            public void  onClick(View v) {

                startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phone, null)));
            }

        });

        Button Email = (Button) findViewById(R.id.button2);
        Email.setOnClickListener(new OnClickListener() {
            //when button2 is clicked open email
            @Override
            public void  onClick(View v) {
                Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { email });
                startActivity(Intent.createChooser(emailIntent, "Choose Email Client"));

            }

        });

        Button Phone = (Button) findViewById(R.id.button3);
        Phone.setOnClickListener(new OnClickListener() {
            //when button3 is clicked start call
            @Override
            public void  onClick(View v) {

                startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null)));
            }

        });


        // get details passed from MainActivity
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            image= bundle.getInt("image");
            ImageView imageView = (ImageView) findViewById(R.id.imageView1);    
            imageView.setImageResource(image);

            name = bundle.getString("name");
            TextView nameView = (TextView) findViewById(R.id.name1);    
            nameView.setText(name);

            address = bundle.getString("address");
            TextView addressView = (TextView) findViewById(R.id.address1);    
            addressView.setText(address);

            phone = bundle.getString("phone");
            TextView phoneView = (TextView) findViewById(R.id.phone1);    
            phoneView.setText(phone);

            email = bundle.getString("email");
            TextView emailView = (TextView) findViewById(R.id.email1);    
            emailView.setText(email);

            note = bundle.getString("note");
            TextView noteView = (TextView) findViewById(R.id.note1);    
            noteView.setText(note);

        }

    }
}

我的自定义视图

package com.example.androidlistview;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomView extends ArrayAdapter<String>{

    private final Activity context;
    private final String[] values;
    private final Integer[] images;

    //Constructor
    public CustomView(Activity context,String[] values, Integer[] images) {

            super(context, R.layout.single_row, values);
            this.context = context;
            this.values = values;
            this.images = images;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.single_row, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        txtTitle.setText(values[position]);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        imageView.setImageResource(images[position]);
        return rowView;
    }
}