我在textview中设置文本时遇到空指针异常。 textview位于仪表板活动的导航视图中。
在主要活动中,我试图设置这样的文字:
public class Dashboard extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
TextView StudentNameView = (TextView) navigationView.findViewById(R.id.studentName);
StudentNameView.setText("Test");
}
}
这是我的信息中心活动的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>
我的导航标题菜单是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
android:gravity="bottom">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing" android:text=""
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:id="@+id/studentName"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="" android:id="@+id/studentEmail" />
</LinearLayout>
答案 0 :(得分:16)
更重要的是使用lib版本v7:23.1.1 使用compile&#39; com.android.support:appcompat-v7:23.1.1&#39;
在你的代码中写下以下行
import java.util.Arrays;
public class RangeTest {
public static void main(String[] args) {
int[] a = new int[] {0,1,2,3,4,5,6,7,8,9};
int[] b = Arrays.copyOfRange(a, Integer.MIN_VALUE, 10);
// If we'll use Integer.MIN_VALUE+100 instead Integer.MIN_VALUE,
// OutOfMemoryError will be thrown
for (int k = 0; k < b.length; k++)
System.out.print(b[k] + " ");
}
}
答案 1 :(得分:2)
将R.layout。NAME_OF_LAYOUT
替换为TextView为
查看navView = LayoutInflater.from(this).inflate(R.layout.NAME_OF_LAYOUT,null);
public class Dashboard extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
View navView = LayoutInflater.from(this).inflate(R.layout.NAME_OF_LAYOUT, null); //navigation header menu layout
TextView StudentNameView = (TextView) navView.findViewById(R.id.studentName);
StudentNameView.setText("Test");
}
}
编辑:
public class Dashboard extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
View navView = LayoutInflater.from(this).inflate(R.layout.NAME_OF_LAYOUT, null); //navigation header menu layout
TextView StudentNameView = (TextView) navView.findViewById(R.id.studentName);
StudentNameView.setText("Test");
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.addHeaderView(navView);
}
}
答案 2 :(得分:1)
@using (Ajax.BeginForm("Foo1", "FooController", new AjaxOptions { OnSuccess = "HandlePostback" }))
{
<input type="hidden" name="pageId" id="pageId" />
<input type="submit" value="Save" id="btnsubmit" />
}
[HttpPost]
public ActionResult Foo1(int pageId)
{
return View();
}
答案 3 :(得分:0)
在获得NavigationView
参考之前,您是否已获取View
和TextView
的引用? (像这样的东西)
android.support.design.widget.NavigationView navigationView= (NavigationView) findViewById (R.id.nav_view);
View view = navigationView.getHeaderView(0);
答案 4 :(得分:0)
做我写的,也许它会帮助你的朋友
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View v = navigationView.getHeaderView(0);
txt_nama = (TextView) v.findViewById(R.id.tv_nama);
txt_nama.setText("Halo, " + nama);