私はドロワーメニューを作ろうとしていますが、NullPointerを持っていますが、すべてがコアctだと思います。NavigationViewでNullPointerExceptionを取得する
私はここにすべての私のコードを投稿します:
package wagner.com.meuartesanato.home;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import wagner.com.meuartesanato.R;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showHome(null);
TextView textMenu = (TextView) findViewById(R.id.text_menu);
TextView textHome = (TextView) findViewById(R.id.text_home);
TextView textSimulation = (TextView) findViewById(R.id.text_simulation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
loadDrawer();
}
public void showMenu(View v){
//drawer.openDrawer(GravityCompat.END);
}
public void showHome(View v){
//loadFragment(Home1Fragment.class);
}
public void showSimulation(View v){
Toast.makeText(this,"Mostrar simulação",Toast.LENGTH_SHORT).show();
}
private void loadDrawer(){
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View view = navigationView.getHeaderView(0);
Button about = (Button)view.findViewById(R.id.button_about);
Button howToUse = (Button)view.findViewById(R.id.button_how_to_use);
Button addAccount = (Button)view.findViewById(R.id.button_add_account);
Button changeRegister = (Button)view.findViewById(R.id.button_change_register);
Button changePassword = (Button)view.findViewById(R.id.button_change_password);
Button help = (Button)view.findViewById(R.id.button_help);
Button exit = (Button)view.findViewById(R.id.button_exit);
about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Mostra tela sobre o app
//loadFragment(SobreFragment.class);
drawer.closeDrawer(GravityCompat.END);
}
});
howToUse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar como usar",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
addAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Mostrar algo",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changeRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
changePassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Alterar cadastro",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"Help",Toast.LENGTH_SHORT).show();
drawer.closeDrawer(GravityCompat.END);
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//fecha a activity, voltando pra tela de login
finish();
}
});
}
private void loadFragment(Class fragmentClass){
try {
Fragment fragment = (Fragment) fragmentClass.newInstance();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_holder, fragment).commit();
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawer.closeDrawer(GravityCompat.END);
return false;
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.END)) {
drawer.closeDrawer(GravityCompat.END);
} else {
drawer.openDrawer(GravityCompat.END);
}
}
}
そして、私のxmlファイル:
activity_home.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/activity_main"
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="end"
app:headerLayout="@layout/nav_header_main"
android:fitsSystemWindows="true"
android:background="@color/colorWhite"/>
</android.support.v4.widget.DrawerLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:background="@drawable/degrade"
tools:context="wagner.com.meuartesanato.home.MainActivity">
<LinearLayout
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top_bar">
<LinearLayout
android:onClick="showMenu"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/menu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_menu"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:textColor="#FFF"
android:text="Menu"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:onClick="showHome"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/home"/>
<TextView
android:id="@+id/text_home"
android:layout_marginTop="5dp"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Home"
/>
</LinearLayout>
<LinearLayout
android:onClick="showSimulation"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/simulacao"
/>
<TextView
android:id="@+id/text_simulation"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:textSize="12sp"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Simulação"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@id/top_bar">
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:background="@drawable/shadow"/>
<FrameLayout
android:id="@+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:layout_marginBottom="@dimen/activity_vertical_margin">
</FrameLayout>
</LinearLayout>
</RelativeLayout>
nav_header_mainを.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#FFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/drawer_avatar"
android:layout_marginTop="20dp"
android:layout_width="115dp"
android:layout_height="115dp" />
<TextView
android:id="@+id/full_name"
android:layout_width="180dp"
android:gravity="center"
android:textColor="@color/colorDarkGray"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginBottom="20dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorLightGray"
android:text="Sobre o App"
android:id="@+id/button_about"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorWhite"
android:text="Como usar o App"
android:id="@+id/button_how_to_use"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorLightGray"
android:text="Adicionar Conta Bancaria?"
android:id="@+id/button_add_account"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorWhite"
android:text="Alterar Cadastro?"
android:id="@+id/button_change_register"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorLightGray"
android:text="Alterar Senha?"
android:id="@+id/button_change_password"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorWhite"
android:text="Ajuda"
android:id="@+id/button_help"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorLightGray"
android:text="Sair"
android:id="@+id/button_exit"
android:textColor="@color/colorPurple"
android:textSize="@dimen/normal_text"
android:textAllCaps="false"
/>
<TextView
android:layout_marginTop="50dp"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copyright"
android:textColor="@color/colorGray"
/>
</LinearLayout>
エラー・ログには、次のとおりです。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: wagner.com.meuartesanato, PID: 9112
java.lang.RuntimeException: Unable to start activity ComponentInfo{wagner.com.meuartesanato/wagner.com.meuartesanato.home.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView$OnNavigationItemSelectedListener)'
on a null object reference
at wagner.com.meuartesanato.home.MainActivity.loadDrawer(MainActivity.java:59)
at wagner.com.meuartesanato.home.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
私は、nullポインタ例外を取得していますなぜ私は本当に知りません。
ありがとうございました!
の可能性のある重複(HTTP [NullPointerExceptionがある、と私はそれをどのように修正すればよいですか?] ://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Isaac
'at wagner.com.meuartesanato.home.MainActivity.loadDrawer(MainActivity.java: 59)これはMainActivity.javaの59行目でエラーが発生したことを示しています。そこから始めよう。 –