错误:CustomBaseAdapter

时间:2016-09-25 08:45:32

标签: java android listview nullpointerexception baseadapter

我在android中创建简单的联系人应用程序。 MainActivity由显示图片和名称的listview组成。我使用模型设置和获取数据,ArrayList将数据传递给下一个activity。当我点击单个listview项目时。它应显示下一个显示联系人详细信息的activity

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    String[] name={
            "Adam",
            "John",
            "Jasmine"
    };
    int[] image={
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3
    };
    String[] mobile_no={
            "03349875429",
            "03458294792",
            "03318245801",
    };

    String[] email={
            "adam123@yahoo.com",
            "john123@gmail.com",
            "jasmine456@hotmail.com"
    };

    String[] url={
            "https://www.google.com.pk/",
            "https://www.google.com.pk/",
            "https://www.google.com.pk/"
    };

    ContactModel contact;
    ArrayList<ContactModel> contact_list=new ArrayList<ContactModel>();
    ListView ls;


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

        ls=(ListView)findViewById(R.id.listView);

        for(int i=0;i<name.length;i++){
            contact=new ContactModel();
            contact.setImage(image[i]);
            contact.setName(name[i]);
            contact.setMobile_no(mobile_no[i]);
            contact.setEmail(email[i]);
            contact.setUrl(url[i]);
            contact_list.add(contact);

        }
        CustomBaseAdapter adapter=new CustomBaseAdapter(this,contact_list);
        ls.setAdapter(adapter);

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContactModel contacts=contact_list.get(i);
                Intent intent=new Intent(getApplicationContext(),ContactDetails.class);
                intent.putExtra("contacts",contacts);
                startActivity(intent);

            }
        });
    }
}

CustomeBaseAdapter.java

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomBaseAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<ContactModel> contact;
    LayoutInflater inflator;
    TextView name;
    ImageView image;

    public CustomBaseAdapter(Activity activity, ArrayList<ContactModel> contact) {
        this.activity=activity;
        this.contact=contact;
        inflator=activity.getLayoutInflater();
    }

    @Override
    public int getCount() {
        return contact.size();
    }

    @Override
    public Object getItem(int i) {
        return contact.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=view;
        if(view==null){
            v=inflator.inflate(R.layout.contact_list_,null);
        }
        name=(TextView)v.findViewById(R.id.name_textView);
        image=(ImageView)v.findViewById(R.id.imageView);


        ContactModel c=(ContactModel) getItem(i);

        name.setText(c.getName());
        image.setImageResource(c.getImage());


        return v;
    }
}

ContactDetails.java

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactDetails extends AppCompatActivity {

    ImageView imageView;
    TextView nameView;
    TextView mobileView;
    TextView emailtextview;
    TextView urlView;
    ImageView callButton;
    ImageView messageButton;
    ImageView emailButton;
    ImageView websiteButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);

        Intent intent=getIntent();
        final ContactModel contacts=(ContactModel)intent.getSerializableExtra("contacts");

        imageView=(ImageView)findViewById(R.id.imageView);
        nameView=(TextView)findViewById(R.id.name_view);
        mobileView=(TextView)findViewById(R.id.number_view);
        emailtextview=(TextView)findViewById(R.id.email_textview);
        urlView=(TextView)findViewById(R.id.url_textview);

        //Buttons
        callButton=(ImageView)findViewById(R.id.callButton);
        messageButton=(ImageView)findViewById(R.id.message_view);
        emailButton=(ImageView)findViewById(R.id.email_imageview);
        websiteButton=(ImageView)findViewById(R.id.url_imageview);

        imageView.setImageResource(contacts.getImage());
        nameView.setText(contacts.getName());
        mobileView.setText(contacts.getMobile_no());
        emailtextview.setText(contacts.getEmail());
        urlView.setText(contacts.getUrl());
    }
}

ContactModel只包含setter和getter方法。呼叫按钮等功能尚未实现。没有显示错误,但是当我运行项目时,它显示1 error: Error:java.lang.NullPointerException(没有错误消息)

logcat的:

09-25 05:09:54.006 1745-1775/system_process E/BluetoothAdapter: Bluetooth binder is null
09-25 05:11:00.001 1745-1773/system_process I/ProcessStatsService: Prepared write state in 0ms
09-25 05:16:38.974 98-98/? D/Genyd: Received Set Clipboard
09-25 05:16:38.974 98-98/? D/Genymotion: Received Set Clipboard

2 个答案:

答案 0 :(得分:0)

您可以这样做来解决您的问题:

ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //ContactModel contacts=new ContactModel();//here you did mistake
                Intent intent=new Intent(getApplicationContext(),ContactDetails.class);
                intent.putExtra("contacts",(Serializable) adapterView.getSelectedItem());
                startActivity(intent);

            }
        });

答案 1 :(得分:0)

ls.setOnItemClickListener中,您传递ContactModel contacts=new ContactModel();,这在技术上是ContactModel的空白对象。

现在,当您尝试在ContactDetails.java中设置来自您从Intent获取的空白对象中的数据时,

imageView.setImageResource(contacts.getImage()); nameView.setText(contacts.getName());

联系人对象没有先前在其上设置的图像或名称(或其他属性)。因此,您将获得空指针异常。

因此,如下所示进行更改,您的问题就会消失。

ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ContactModel contacts= contact_list.get(i);
                Intent intent=new Intent(getApplicationContext(),ContactDetails.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("contacts",contacts);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });

在ContactDetails.java中,获取这样的对象。

Intent intent=getIntent();
Bundle bundle = intent.getExtras();

final ContactModel contacts=(ContactModel)bundle.getSerializable("contacts");