I wanted to create a ListView
in a Fragment
. I have created a custom adapter for a an array of objects of data type ArrayList
.
But the app crashes when I launch the activity containing this Article
fragment.
Error
12-31 12:01:09.746 25620-25620/com.example.root.talaash E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.talaash, PID: 25620
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.root.talaash/com.example.root.talaash.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.access$800(ActivityThread.java:167)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.root.fragments.ArticleFragment.onCreateView(ArticleFragment.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
at android.app.Activity.performStart(Activity.java:6068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
at android.app.ActivityThread.access$800(ActivityThread.java:167)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5309)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Article class(package : com.example.root.aditional)
public class Article {
String title;
String author;
String category;
String date;
String url;
Boolean tag = false;
public Article(String title, String author, String category, String date, String url) {
this.title = title;
this.author = author;
this.category = category;
this.date = date;
this.url = url;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getCategory() {
return category;
}
public String getDate() {
return date;
}
public void setTag(Boolean tag) {
this.tag = tag;
}
public String getUrl() {
return url;
}
}
ArticleAdaper class(package : com.example.root.adapters)
public class ArticleAdapter<A> extends ArrayAdapter<Article>{
private static class ViewHolder {
TextView title, author, category, date;
}
public ArticleAdapter(Context context, ArrayList<Article> articles) {
super(context, R.layout.row_article, articles);
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null) {
convertView = LayoutInflater.from(this.getContext()).inflate(R.layout.row_article, parent, false);
viewHolder.title = (TextView)convertView.findViewById(R.id.titleText);
viewHolder.author= (TextView)convertView.findViewById(R.id.authorText);
viewHolder.category= (TextView)convertView.findViewById(R.id.categoryText);
viewHolder.date = (TextView)convertView.findViewById(R.id.dateText);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
Article item = getItem(position);
if(item!= null){
viewHolder.title.setText(item.getTitle());
viewHolder.author.setText(item.getAuthor());
viewHolder.category.setText(item.getCategory());
viewHolder.date.setText(item.getDate());
}
return convertView;
}
}
I wrote the above class with the help of reading this question's first answer : How to use ArrayAdapter<myClass>
The fragment containing the listView(package : com.example.root.project)
public class ArticleFragment extends Fragment {
public ArticleFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<Article> articles = new ArrayList<>(6);
articles.add(new Article("Rose", "Sample Author", "Sample category", "Sample date", "Sample url"));
ListAdapter articleAdapter = new ArticleAdapter(this.getContext(), articles);
ListView listView = (ListView)container.findViewById(R.id.articleListView);
listView.setAdapter(articleAdapter);
/*ListView listView = (ListView)findViewById(R.id.articleListView);*/
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_article, container, false);
}
}
I am very new to android development and I would like to have a detailed explanation as to where I am going wrong and what I should do to fix it.