java为什么findViewById返回null?
为什么我的findViewById在这里返回nullswipeRefreshLayout = findViewById(R.id.swipeRefreshLayoutDashboard);
我已经尝试过使用.getRootView()
并自己沿着层次结构往下走,但没有成功。你能帮帮我吗
仪表板活动。爪哇
package eu.niehus.app;
import 安卓.app.AlertDialog;
import 安卓.app.DialogFragment;
import 安卓.content.DialogInterface;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.support.annotation.NonNull;
import 安卓.support.design.widget.FloatingActionButton;
import 安卓.support.design.widget.NavigationView;
import 安卓.support.v4.view.GravityCompat;
import 安卓.support.v4.widget.DrawerLayout;
import 安卓.support.v4.widget.SwipeRefreshLayout;
import 安卓.support.v7.app.ActionBarDrawerToggle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.support.v7.widget.Toolbar;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.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();
}
});
}
}
活动面板。xml:
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v4.widget.DrawerLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/drawer_layout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_dashboard"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
<安卓.support.design.widget.NavigationView
安卓:id="@+id/nav_view"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:layout_gravity="start"
安卓:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_dashboard"
app:menu="@menu/activity_dashboard_drawer" />
</安卓.support.v4.widget.DrawerLayout>
应用程序工具栏仪表板。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".DashboardActivity"
安卓:id="@+id/constraintLayout">
<安卓.support.design.widget.AppBarLayout
安卓:layout_width="0dp"
安卓:theme="@style/AppTheme.AppBarOverlay"
安卓:id="@+id/appBarLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
安卓:layout_height="wrap_content">
<安卓.support.v7.widget.Toolbar
安卓:id="@+id/toolbar"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</安卓.support.design.widget.AppBarLayout>
<include
layout="@layout/content_dashboard"
安卓:layout_height="0dp"
安卓:layout_width="0dp"
安卓:id="@+id/include"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<安卓.support.design.widget.FloatingActionButton
安卓:id="@+id/fab"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
app:srcCompat="@安卓:drawable/ic_input_add"
app:layout_constraintEnd_toEndOf="parent"
安卓:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
安卓:layout_marginBottom="8dp"/>
</安卓.support.constraint.ConstraintLayout>
内容面板。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.v4.widget.SwipeRefreshLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/swipeRefreshLayoutDashboard">
<ScrollView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<TableLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<TableRow
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:id="@+id/tableRowWeighGraph"
安卓:minHeight="200dp">
<com.github.mikephil.charting.charts.LineChart
安卓:id="@+id/lineChart"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_weight="999"/>
</TableRow>
</TableLayout>
</ScrollView>
</安卓.support.v4.widget.SwipeRefreshLayout>
# 1 楼答案
尝试进行转换:
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoutDashboard);
# 2 楼答案
我已经解决了这个问题,在我的app\u bar\u仪表板内部的“include”中添加了一个android:id参数。xml并使用它来获取我的布局
以下是更新的源代码:
仪表板活动。爪哇:
活动面板。xml:
应用程序工具栏仪表板。xml:
内容面板。xml:
# 3 楼答案
如果在另一个布局中包含布局,首先需要使用findViewById为包含的布局创建Id为的对象。然后在其上调用findViewById进行滑动以刷新布局