0
這是我已經完成我的登錄代碼,並在登錄後,它提取用戶的名稱和電子郵件鞋類。 獲取過程運行良好,但是當我嘗試設置不同佈局xml文件的文本時,它給了我空指針異常。我嘗試從抓取數據庫設置textview的文本,但我無法做到這一點
這是NavigationMenu類這裏是我的登錄功能
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
RoundImage roundedImage;
ImageView profileImage;
TextView uname,mail;
private SQLiteHandler db;
private SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_menu);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new SQLiteHandler(getApplicationContext());
HashMap<String,String> user = db.getUserDetails();
String name = user.get("name");
String email = user.get("email");
Log.d("Soulsystem", name);
Log.d("Soulsystem", email);
uname = (TextView)findViewById(R.id.alluser);
mail = (TextView) findViewById(R.id.allemail);
try {
uname.setText(name);
mail.setText(email);
}catch (NullPointerException NPE)
{
Log.d("Soul system","Here is the error we are setting null values");
}
/* profileImage = (ImageView)view.findViewById(R.id.navigation_view_Image);
profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("NavigationMenu", "In profile Image view");
Intent i = new Intent(NavigationMenu.this, ClientProfile.class);
}
});*/
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
navigationView = (NavigationView)findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.profile:
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new ClientProfile());
fragmentTransaction.commit();
item.setChecked(true);
drawerLayout.closeDrawers();
break;
case R.id.logout:
logoutUser();
/* item.setChecked(true);
drawerLayout.closeDrawers();*/
break;
}
return true;
}
});
}
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
actionBarDrawerToggle.syncState();
super.onPostCreate(savedInstanceState);
}
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
Intent intent = new Intent(NavigationMenu.this, MainActivity.class);
startActivity(intent);
finish();
}
}
而這一次在這裏的導航菜單的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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context="com.example.user.soulsystem.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/main_container">
<TextView
android:id="@+id/alluser"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:gravity="center_horizontal"
android:id="@+id/allemail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/navigation_view"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/drawe_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
這一次,我想表明我怎麼能做到這一點 我試圖使用佈局聯運文本的XML,但是當我試圖設置文本它給我上的setText()空指針異常
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:background="@color/bg_register">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/navigation_view_Image"
android:background="@drawable/image"
android:src="@drawable/profile_image"
android:layout_marginLeft="30dp"
android:layout_marginTop="10dp"/>
<TextView
android:layout_below="@id/navigation_view_Image"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/username"
android:textColor="@color/Black"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:layout_alignLeft="@+id/navigation_view_Image"
android:layout_alignStart="@+id/navigation_view_Image" />
</LinearLayout>
<TextView
android:layout_below="@id/username"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="@+id/email"
android:textColor="@color/Black"
android:layout_alignLeft="@+id/navigation_view_Image"
android:layout_alignStart="@+id/navigation_view_Image" />
</LinearLayout>
*「它給了我空指針異常」* - 好的,然後顯示logcat。關於錯誤的情況不清楚問題存在的地方? –
引起:java.lang.NullPointerException:試圖在com.example.user.soulsystem.NavigationMenu.onCreate()上的空對象引用上調用虛擬方法'void android.widget.TextView.setText(java.lang.CharSequence)' Navigati onMenu.java:57) –
當我從第二個xml文件調用用戶名文本視圖時發生錯誤 –