正确实现ListView的addFooterView(Properly implement ListView's addFooterView)

编程入门 行业动态 更新时间:2024-10-24 04:49:17
正确实现ListView的addFooterView(Properly implement ListView's addFooterView)

我一直在从一门课程学习Android开发,我们制作了一个笔记应用程序。 我完成了它,我想添加内容并改进应用程序。 我尝试向ListView添加页脚但我不断收到NullPointerException: Unable to instantiate activity ComponentInfo

这是我的代码:

public TextView mTextView = (TextView) findViewById(R.id.footer_text); final ListView listView = (ListView) findViewById(R.id.task_list); mTaskAdapter = new TaskAdapter(mTasks); listView.setAdapter(mTaskAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mLastPositionClicked = position; // Toast.makeText(getApplicationContext(), "Task " + (position + 1) + " was pressed", Toast.LENGTH_SHORT).show(); Intent i = new Intent(TaskListActivity.this, TaskActivity.class); Task task = (Task) parent.getAdapter().getItem(position); i.putExtra(TaskActivity.EXTRA, task); startActivityForResult(i, EDIT_TASK_REQUEST); } }); listView.addFooterView(mTextView);

和XML:

<TextView android:id="@+id/footer_text" android:text="Works!!!" android:layout_width="match_parent" android:layout_height="wrap_content"/>

我不知道我做错了什么。 另外,如果格式化很奇怪,我道歉。 这对我来说是新的。 这是要点: https : //gist.github.com/najubhai/dcff0ea72feea14673a8

I've been learning Android development from a course and we made a note taking app. I finished it and I want to add stuff and improve the app. I tried adding a footer to the ListView but I keep getting a NullPointerException: Unable to instantiate activity ComponentInfo

Here is the code I have:

public TextView mTextView = (TextView) findViewById(R.id.footer_text); final ListView listView = (ListView) findViewById(R.id.task_list); mTaskAdapter = new TaskAdapter(mTasks); listView.setAdapter(mTaskAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mLastPositionClicked = position; // Toast.makeText(getApplicationContext(), "Task " + (position + 1) + " was pressed", Toast.LENGTH_SHORT).show(); Intent i = new Intent(TaskListActivity.this, TaskActivity.class); Task task = (Task) parent.getAdapter().getItem(position); i.putExtra(TaskActivity.EXTRA, task); startActivityForResult(i, EDIT_TASK_REQUEST); } }); listView.addFooterView(mTextView);

And the XML:

<TextView android:id="@+id/footer_text" android:text="Works!!!" android:layout_width="match_parent" android:layout_height="wrap_content"/>

I don't know what I'm doing wrong. Also, I apologize if the formatting is weird. This is new to me. Here's the gist: https://gist.github.com/najubhai/dcff0ea72feea14673a8

最满意答案

在Activity有一个窗口之前调用findViewById() ,该方法应该在onCreate(Bundle)之后或之后调用。 更改您的代码如下将有助于:

final ListView listView = (ListView) findViewById(R.id.task_list); // Move the addFooterView line to here, because this method // could only be called before setting the adapter with setAdapter before KITKAT(Android 4.4) listView.addFooterView(LayoutInflater.from(this).inflate(R.layout.your_footer_layout, null)); mTaskAdapter = new TaskAdapter(mTasks); listView.setAdapter(mTaskAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mLastPositionClicked = position; // Toast.makeText(getApplicationContext(), "Task " + (position + 1) + " was pressed", Toast.LENGTH_SHORT).show(); Intent i = new Intent(TaskListActivity.this, TaskActivity.class); Task task = (Task) parent.getAdapter().getItem(position); i.putExtra(TaskActivity.EXTRA, task); startActivityForResult(i, EDIT_TASK_REQUEST); } });

Your call findViewById() before an Activity has a window, that method should be called in or after onCreate(Bundle). Change your code as following will help:

final ListView listView = (ListView) findViewById(R.id.task_list); // Move the addFooterView line to here, because this method // could only be called before setting the adapter with setAdapter before KITKAT(Android 4.4) listView.addFooterView(LayoutInflater.from(this).inflate(R.layout.your_footer_layout, null)); mTaskAdapter = new TaskAdapter(mTasks); listView.setAdapter(mTaskAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mLastPositionClicked = position; // Toast.makeText(getApplicationContext(), "Task " + (position + 1) + " was pressed", Toast.LENGTH_SHORT).show(); Intent i = new Intent(TaskListActivity.this, TaskActivity.class); Task task = (Task) parent.getAdapter().getItem(position); i.putExtra(TaskActivity.EXTRA, task); startActivityForResult(i, EDIT_TASK_REQUEST); } });

更多推荐

本文发布于:2023-04-29 09:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   ListView   addFooterView   Properly   implement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!