我正在尝试用android中的自定义适配器编写ListView。当我的代码完成时,我得到了'java.lang.NullPointerException'。 我一次又一次检查但无法修复此错误。
这是MainActivity代码:
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
在MainActivity代码中我创建了转到HomeActivity的意图。 HomeActivity代码:
package com.example.sayres.myapplication3_listview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
private EditText mainActivity_editText_userName, mainActivity_editText_password;
private Button mainActivity_btn_login, mainActivity_btn_exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mainActivity_editText_userName = (EditText) findViewById(R.id.mainActivity_editText_userName);
mainActivity_editText_password = (EditText) findViewById(R.id.mainActivity_editText_password);
mainActivity_btn_login = (Button) findViewById(R.id.mainActivity_btn_login);
mainActivity_btn_exit = (Button) findViewById(R.id.mainActivity_btn_exit);
mainActivity_btn_exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mainActivity_btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginprocess();
}
});
}
private void loginprocess() {
String userName = mainActivity_editText_userName.getText().toString();
String password = mainActivity_editText_password.getText().toString();
Log.i("====>", "UserName= "+ userName+" Passwotd: "+ password);
Toast.makeText(getApplicationContext(),userName, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
}
}
我在这行中遇到错误:package com.example.sayres.myapplication3_listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.sayres.myapplication3_listview.adapter.ContactAdapter;
import com.example.sayres.myapplication3_listview.model.Contact;
import java.util.List;
public class HomeActivity extends AppCompatActivity {
private ListView listViewHome;
private List<Contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initViews();
}
private void initViews() {
listViewHome = (ListView) findViewById(R.id.listViewHome);
for (int i = 0; i < 1000; i++) {
int pic = 0;
if (i % 2 == 0) {
pic = R.drawable.man;
} else {
pic = R.drawable.female;
}
contacts.add(createContact("name " + i, "family " + i, "5526576", pic));
}
ContactAdapter adapter = new ContactAdapter(this, R.layout.row_contacts_list, contacts);
listViewHome.setAdapter(adapter);
}
private Contact createContact(String name, String family, String number, int picture) {
return new Contact(name, family, number, picture);
}
}
这个错误
contacts.add(createContact("name " + i, "family " + i, "5526576", pic));
这是ContactAdapter扩展ArrayAdapter代码:
at com.example.sayres.myapplication3_listview.HomeActivity.initViews(HomeActivity.java:34)
at com.example.sayres.myapplication3_listview.HomeActivity.onCreate(HomeActivity.java:21)
我的Contact类是一个带有setter和getter的简单类:
package com.example.sayres.myapplication3_listview.adapter;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.example.sayres.myapplication3_listview.R;
import com.example.sayres.myapplication3_listview.model.Contact;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private int layout;
private List<Contact> contacts;
public ContactAdapter(Context context, int layout, List<Contact> contacts) {
super(context, layout, contacts);
this.context = context;
this.layout = layout;
this.contacts = contacts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(layout, null);
if (position % 2 == 0 ){
rootView.setBackgroundColor(Color.parseColor("#12ffff"));
}else {
rootView.setBackgroundColor(Color.parseColor("#FFFF12"));
}
/**
* set reference from row_contacts_list.xml
*/
TextView contactList_name = (TextView) rootView.findViewById(R.id.contactList_name);
TextView contactList_family = (TextView) rootView.findViewById(R.id.contactList_family);
CircleImageView profile_picture = (CircleImageView) rootView.findViewById(R.id.profile_picture);
contactList_name.setText(contacts.get(position).getContactName());
contactList_family.setText(contacts.get(position).getContactFamily());
profile_picture.setImageResource(contacts.get(position).getContactPicture());
return rootView;
}
}
我的错误在哪里?
答案 0 :(得分:3)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Init the contacts
contacts = new ArrayList<Contact>();
initViews();
}