我正在构建登录屏幕,其中包含详细信息。我试图将这些细节放在导航视图中,您可以在其中看到您的姓名电子邮件。我使用共享偏好保存了详细信息。 问题是当我更新这些视图时,它会抛出NULL POINT EXCEPTION。那么,如何在MAIN活动中初始化另一个布局的视图
我的日志
07-05 12:02:39.549 24377-24377/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: rishabh.example.com.navigationdrawer, PID: 24377
java.lang.RuntimeException: Unable to start activity ComponentInfo{rishabh.example.com.navigationdrawer/rishabh.example.com.navigationdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
at rishabh.example.com.navigationdrawer.MainActivity.onCreate(MainActivity.java:58)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
MainActivity的部分代码正在初始化视图。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.base_navbar_id);
LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.navigation_drawer_head,null);
name1= (TextView)v.findViewById(R.id.name_navDrawer);
//login window which runs only one time when app is installed
myPreferences = new MyPreferences();
if (MyPreferences.isFirst(this)) {
Intent intent=new Intent(this,LoginActivity.class);
startActivity(intent);
}
SharedPreferences sharedPreferences=((DataProvider)getApplicationContext()).getSharedPreferences("MY_LOGIN",MODE_PRIVATE);
if(sharedPreferences.getBoolean("IS_TRUE",false)) {
Log.i("tag", "data:" + sharedPreferences.getString("MY_NAME", "Name"));
name1.setText(sharedPreferences.getString("MY_NAME", "Name"));
linearLayout.addView(name1);
}
我的navigation_drawer_head.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="140dp"
android:id="@+id/base_navbar_id"
android:background="@android:color/holo_orange_light">
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:id="@+id/imageView"
android:src="@drawable/my_image"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="56dp"
android:layout_marginStart="56dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="7"
android:layout_marginTop="30dp"
android:hint="Name"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textSize="25dp"
android:id="@+id/name_navDrawer" />
</LinearLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
tools:context="rishabh.example.com.navigationdrawer.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container_fragment">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/navView"
android:layout_gravity="start"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/navigation_drawer_head"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
正如它在例外中提到的那样,
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
这意味着linearLayout
为null
。这是由findViewById
无法找到R.id.base_navbar_id
的任何视图引起的。
答案 1 :(得分:0)
替换下面的行,
name1= (TextView)v.findViewById(R.id.name_navDrawer);
通过
name1= (TextView) findViewById(R.id.name_navDrawer);
然后检查它是否有效。
主要的是它已经存在于主xml文件中,因此它处于线性布局中,因此还要检查是否要从抽屉xml文件中添加另一个文本视图,以便检查它。
现在在下面进行更改,
LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.navigation_drawer_head,null);
linearLayout=(LinearLayout) v.findViewById(R.id.base_navbar_id);