我想在ListView
中创建Fragment
。我为数据类型为ArrayList
的对象数组创建了一个自定义适配器。
但是,当我启动包含此Article
片段的活动时,应用会崩溃。
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)
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;
}
}
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;
}
}
我在阅读这个问题的第一个答案的帮助下写了上面的课: How to use ArrayAdapter<myClass>
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);
}
}
我对Android开发非常陌生,我想详细解释我哪里出错了以及我该怎么做才能解决它。
答案 0 :(得分:1)
当应用程序尝试使用时抛出NullPointerException 具有空值的对象引用。
首先返回您的观点。
不要
Sub finddata()
With Sheets("Sheet1")
.Range("P3:X37").ClearContents
With .Range("B1", .Cells(.Rows.count, 2).End(xlUp)) '<--| reference column "B" range from row 1 (header) down to last not empty row
.AutoFilter field:=1, Criteria1:=Application.Transpose(.Parent.Range("K8:K30").Value), Operator:=xlFilterValues '<--| filter on all K8:K30 values
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any filtered cell found
.Offset(1, -1).Resize(.Rows.count - 1, 3).SpecialCells(xlCellTypeVisible).Copy '<-- copy filtered range offsetted one column to the right and resized to three columns
.Parent.Cells(.Rows.count, "P").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats '<--| paste special
End If
End With
.AutoFilterMode = False
End With
End Sub
待办事项
return inflater.inflate(R.layout.fragment_article, container, false);
然后传递 getActivity()
实施例
return your view ;
答案 1 :(得分:0)
您正试图在错误CreateProcessAsUser
内找到ListView
。试试这个 -
ViewGroup
答案 2 :(得分:0)
您需要先在ListView
中对视图进行充气,然后您需要找到container
而不是onCreateView
。最后将视图作为public class ArticleFragment extends Fragment {
public ArticleFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_article, container, false);
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) v.findViewById(R.id.articleListView);
listView.setAdapter(articleAdapter);
// Return the inflated Fragment here
return v;
}
}
中的最后一个return语句返回。
{{1}}
答案 3 :(得分:0)
试试这个:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_article, container, false);
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)view.findViewById(R.id.articleListView);
listView.setAdapter(articleAdapter);
/*ListView listView = (ListView)findViewById(R.id.articleListView);*/
// Inflate the layout for this fragment
return view;
}
您没有正确获取列表视图