This question already has an answer here:
这个问题在这里已有答案:
- What is a NullPointerException, and how do I fix it? 12 answers
什么是NullPointerException,我该如何解决? 12个答案
after writing the app when i ran it iam getting following error
在我运行它时编写应用程序后,我得到以下错误
Unable to start activity ComponentInfo java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
here is my mainactivity.java class
这是我的mainactivity.java类
public class MainActivity extends AppCompatActivity {
ArrayList<Contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecyclerView rvContacts = findViewById(R.id.rvContacts);
// Initialize contacts
contacts = Contact.createContactsList(20);
// Create adapter passing in the sample user data
ContactsAdapter adapter = new ContactsAdapter();
// Attach the adapter to the recyclerview to populate items
rvContacts.setAdapter(adapter);
// Set layout manager to position the items
rvContacts.setLayoutManager(new LinearLayoutManager(this));
// That's all!
}
}
2 个解决方案
#1
1
you need to cast RecyclerView
like this..
你需要像这样投射RecyclerView ..
RecyclerView rvContacts = (RecyclerView)findViewById(R.id.rvContacts);
and also you need to call setContentView(R.id.your_layout)
to set your layout.
你还需要调用setContentView(R.id.your_layout)来设置你的布局。
#2
0
you are missing setContentView()
in your onCreate of MainActivity
你在MainActivity的onCreate中缺少setContentView()
so add line setContentView(R.layout.activity_main);
below super.onCreate(savedInstanceState);
check code
所以添加行setContentView(R.layout.activity_main);在super.onCreate(savedInstanceState)下面;检查代码
public class MainActivity extends AppCompatActivity {
ArrayList<Contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //change here
RecyclerView rvContacts = findViewById(R.id.rvContacts);
// Initialize contacts
contacts = Contact.createContactsList(20);
// Create adapter passing in the sample user data
ContactsAdapter adapter = new ContactsAdapter();
// Attach the adapter to the recyclerview to populate items
rvContacts.setAdapter(adapter);
// Set layout manager to position the items
rvContacts.setLayoutManager(new LinearLayoutManager(this));
// That's all!
}
}
#1
1
you need to cast RecyclerView
like this..
你需要像这样投射RecyclerView ..
RecyclerView rvContacts = (RecyclerView)findViewById(R.id.rvContacts);
and also you need to call setContentView(R.id.your_layout)
to set your layout.
你还需要调用setContentView(R.id.your_layout)来设置你的布局。
#2
0
you are missing setContentView()
in your onCreate of MainActivity
你在MainActivity的onCreate中缺少setContentView()
so add line setContentView(R.layout.activity_main);
below super.onCreate(savedInstanceState);
check code
所以添加行setContentView(R.layout.activity_main);在super.onCreate(savedInstanceState)下面;检查代码
public class MainActivity extends AppCompatActivity {
ArrayList<Contact> contacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //change here
RecyclerView rvContacts = findViewById(R.id.rvContacts);
// Initialize contacts
contacts = Contact.createContactsList(20);
// Create adapter passing in the sample user data
ContactsAdapter adapter = new ContactsAdapter();
// Attach the adapter to the recyclerview to populate items
rvContacts.setAdapter(adapter);
// Set layout manager to position the items
rvContacts.setLayoutManager(new LinearLayoutManager(this));
// That's all!
}
}