【问题标题】:Android SwipeRefreshLayout NULL pointer exception SwipeRefreshLayout.setOnRefreshListener()Android SwipeRefreshLayout NULL 指针异常 SwipeRefreshLayout.setOnRefreshListener()
【发布时间】:2015-11-11 13:04:15
【问题描述】:

我正在尝试将拉动刷新功能添加到列表视图中。

在fragment_page.xml(这里定义了全局listview)

<LinearLayout
    style="@style/AppTheme.BaseActivity"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="xx.view.fragment.PageFragment">

    <TextView
        android:id="@android:id/empty"
        style="@style/AppTheme.WrapContent.NoResult"
        android:text="@string/no_results"/>

    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swiperefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:listSelector="@drawable/list_selector"/>

    </android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

在活动中:

public class MessageActivity extends TabBaseActivity implements ManageView, OnRefreshListener {

    private MessageController mMessageController;
    private int mSelectedTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
        setUp(new String[]{"Unread", "Read", "Sent", "All"}, Constants.Adapter.MESSAGE_ADAPTER);
        mMessageController.loadMessages();

        //START  Swipe to refresh
        try {

            final SwipeRefreshLayout swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
            if(swipeContainer == null) {
                Logger.d("Null");
            }
            swipeContainer.setEnabled(false);
            swipeContainer.setOnRefreshListener(this);

        } catch (Exception e) {
            Logger.e(e.getMessage());
        }
        //END  Swipe to refresh

    }

但 swipeContainer 仍然为空。

我尝试使用以下教程进行操作:

http://www.androidhive.info/2015/05/android-swipe-down-to-refresh-listview-tutorial/

http://www.survivingwithandroid.com/2014/05/android-swiperefreshlayout-tutorial.html

但没有运气。

请问如何解决?

非常感谢您的任何建议。

编辑:

视图的内容在 init 中设置:

private void init() {
        mMessageController = MessageController.getInstance(this, getSupportFragmentManager());
        mMessageController.setView(this);
        mSelectedTab = 0;
    }

【问题讨论】:

  • 您是否设置了内容视图?除非您在 init() 内执行此操作,否则它会丢失。
  • 是的,请检查我更新的问题。
  • 在尝试查找视图元素之前,您需要调用setContentView()
  • 不能在 TabBaseActivity 中使用 setContentView()
  • 如果你不能使用setContentView,你也不能使用findViewById

标签: android listview swiperefreshlayout


【解决方案1】:

如果您想在片段中使用您的视图(SwipeRefreshLayout 等),请在 onActivityCreated() 方法中初始化您的视图。一旦 onActivityCreated() 被调用,您就知道该活动已准备好被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多