【问题标题】:Why does findViewById return null?为什么 findViewById 返回 null?
【发布时间】:2018-04-19 18:40:16
【问题描述】:

为什么我的 findViewById 在这里返回 null swipeRefreshLayout = findViewById(R.id.swipeRefreshLayoutDashboard);

我已经尝试使用.getRootView() 并自己向下层级,但没有任何成功。你能帮帮我吗?

DashboardActivity.java

package eu.niehus.app;

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.EntryXComparator;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
public class DashboardActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment fragment = new AddWeightDialog();
                fragment.show(getFragmentManager(), "fab");
            }
        });

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayoutDashboard);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.i("onRefreshListener", "refreshing");
                update();
            }
        });
    }
}

activity_dashboard.xml:

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_dashboard"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_dashboard"
        app:menu="@menu/activity_dashboard_drawer" />

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

app_bar_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DashboardActivity"
        android:id="@+id/constraintLayout">

    <android.support.design.widget.AppBarLayout
            android:layout_width="0dp"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:id="@+id/appBarLayout"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                />

    </android.support.design.widget.AppBarLayout>

    <include
            layout="@layout/content_dashboard"
            android:layout_height="0dp"
            android:layout_width="0dp"
            android:id="@+id/include"
            app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            />

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_input_add"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp"/>

</android.support.constraint.ConstraintLayout>

content_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/swipeRefreshLayoutDashboard">

    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tableRowWeighGraph"
                    android:minHeight="200dp">

                <com.github.mikephil.charting.charts.LineChart
                        android:id="@+id/lineChart"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="999"/>
            </TableRow>

        </TableLayout>
    </ScrollView>

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

【问题讨论】:

  • 你能在活动中分享你的导入吗,因为我猜你使用了错误的 R.layout 而不是你的应用程序。
  • @vikaskumar 我已经添加了导入
  • 根据另一个用户设置的重复标志,这应该通过删除包含 @SvenNiehus 上的 id 来解决
  • 上述评论可能有效,从包含标签中删除 id 参数

标签: java android xml android-layout


【解决方案1】:

如果您在另一个布局中包含布局,首先您需要使用 findViewById 为包含的布局创建具有其 Id 的对象。然后在其上调用 findViewById 以滑动刷新布局。

【讨论】:

    【解决方案2】:

    我已经通过在 app_bar_dashboard.xml 中的“包含”上添加一个 android:id 参数并使用它来获取我的 SwipeRefreshLayout 来解决它。

    这里是更新的源代码:

    DashboardActivity.java:

    package eu.niehus.app;
    
    import android.app.AlertDialog;
    import android.app.DialogFragment;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.constraint.ConstraintLayout;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import com.github.mikephil.charting.charts.LineChart;
    import com.github.mikephil.charting.components.YAxis;
    import com.github.mikephil.charting.data.Entry;
    import com.github.mikephil.charting.data.LineData;
    import com.github.mikephil.charting.data.LineDataSet;
    import com.github.mikephil.charting.utils.EntryXComparator;
    import eu.niehus.app.common.DBHelper;
    
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.List;
    
    public class DashboardActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {
    
        private ConstraintLayout outer;
        private SwipeRefreshLayout swipeRefreshLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dashboard);
            Toolbar toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogFragment fragment = new AddWeightDialog();
                    fragment.show(getFragmentManager(), "fab");
                }
            });
    
            DrawerLayout drawer = findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
    
            dbHelper = new DBHelper(this);
            outer = findViewById(R.id.outer);
            swipeRefreshLayout = outer.findViewById(R.id.swipeRefreshLayoutInclude);
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    Log.i("onRefreshListener", "refreshing");
                    update();
                }
            });
            lineChart = swipeRefreshLayout.findViewById(R.id.lineChartWeight);
            lineChart.setTouchEnabled(false);
            update();
        }
    }
    

    activity_dashboard.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    
        <include
            android:id="@+id/outer"
            layout="@layout/app_bar_dashboard"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_dashboard"
            app:menu="@menu/activity_dashboard_drawer" />
    
    </android.support.v4.widget.DrawerLayout>
    

    app_bar_dashboard.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".DashboardActivity"
            android:id="@+id/constraintLayout">
    
        <android.support.design.widget.AppBarLayout
                android:layout_width="0dp"
                android:theme="@style/AppTheme.AppBarOverlay"
                android:id="@+id/appBarLayout"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/AppTheme.PopupOverlay"
                    />
    
        </android.support.design.widget.AppBarLayout>
    
        <include
                layout="@layout/content_dashboard"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:id="@+id/swipeRefreshLayoutInclude"
                app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                />
    
        <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/ic_input_add"
                app:layout_constraintEnd_toEndOf="parent"
                android:layout_marginEnd="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_marginBottom="8dp"/>
    
    </android.support.constraint.ConstraintLayout>
    

    content_dashboard.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/swipeRefreshLayoutDashboard">
    
        <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <TableLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                <TableRow
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/tableRowWeighGraph"
                        android:minHeight="200dp">
    
                    <com.github.mikephil.charting.charts.LineChart
                            android:id="@+id/lineChartWeight"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="999"/>
                </TableRow>
    
            </TableLayout>
        </ScrollView>
    
    </android.support.v4.widget.SwipeRefreshLayout>
    

    【讨论】:

      【解决方案3】:

      尝试制作演员表:swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutDashboard);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-27
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 2011-03-16
        • 1970-01-01
        相关资源
        最近更新 更多