我在布局中有一个TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_header"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:background="@drawable/avena_nav_drawer"
android:gravity="bottom"
android:orientation="vertical"
android:padding="@dimen/header_left_padding"
style="@style/AppTheme">
<!-- Correo de la cuenta -->
<TextView
android:id="@+id/drawer_email"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/user_email"
android:textSize="14sp"
android:textColor="@color/chalk"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
现在,这样的布局充当了我的drawerlayout中的标题,如下所示:
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/chalk"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/action_menu" />
我一直在尝试从Activity中引用我的TextView,它扩展了包含NavigationView的布局(也尝试了另一种棘手/失败的方式),但它总是抛出一个空对象引用。
有人可以帮助我吗?
修改
这是logcat:
01-07 22:59:00.149 4284-4284/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.avena.avena, PID: 4284
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.avena.avena/com.avena.avena.dashboard.Dashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.avena.avena.dashboard.Dashboard.onCreate(Dashboard.java:70)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
以下是活动代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Set and reference textview inside drawer
TextView drawer_email = (TextView) findViewById(R.id.drawer_email);
if (logged) {
drawer_email.setText(email);
} else {
drawer_email.setText("Invitado");
}
// Set layout
setContentView(R.layout.activity_main);
编辑2
在这里找到正确的答案:
答案 0 :(得分:1)
您没有夸大您的布局,您应该添加:
using (var cmd = new SqlCommand("select group1 from product where productname = @pn)", con))
{
cmd.Parameters.Add(new SqlParameter("@pn", TextBox1.Text));
reader = cmd.ExecuteReader();//got error
while (reader.Read())
{
data = Convert.ToInt32(reader);//got error
}
}
后:
setContentView(R.layout.yourLayoutNameHere);
答案 1 :(得分:0)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = LayoutInflater.from(context).inflate(R.layout... //whatever your inflating code is
...
// Set and reference textview inside drawer
//here use view to access your item
TextView drawer_email = (TextView) view.findViewById(R.id.drawer_email);
if (logged) {
drawer_email.setText(email);
} else {
drawer_email.setText("Invitado");
}
如果没有设置布局或没有引用视图,则无法访问布局的任何项目。
因此,您需要先设置视图,然后访问textview
答案 2 :(得分:0)
查看当前的代码,似乎是在扩充布局之前尝试访问TextView。
尝试以下方法:
// Set layout
setContentView(R.layout.activity_main);
调用setContentView
后,此API会从布局资源中设置活动内容。资源将被夸大,将所有顶级视图添加到活动中。
在您访问TextView
之前调用此方法。
TextView drawer_email = (TextView) view.findViewById(R.id.drawer_email);
答案 3 :(得分:0)
首先你必须打电话
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
然后:
TextView drawer_email = (TextView) findViewById(R.id.drawer_email);
//and your rest of code..