我收到错误尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()'

时间:2019-06-20 09:17:11

标签: android

我收到此错误“ java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.Class java.lang.Object.getClass()'”。我找不到导致问题的问题。 我试图找到答案,但没有人帮助这个错误

MainActivity.java

    String nama=getIntent().getStringExtra(TAG_NAMA);
    String jabatan=getIntent().getStringExtra(TAG_JABATAN);
    String tlp=getIntent().getStringExtra(TAG_TLP);

    tvJabatan.setText(jabatan);
    tvnama.setText(nama);
    tvTlp.setText(tlp);



    //set Fragmentclass Arguments
    ProfileFragment profileFragment=new ProfileFragment();


    bottomNavigation = (BottomNavigationView) findViewById(R.id.bn_main);
    fragmentManager = getSupportFragmentManager();

    //Untuk inisialisasi fragment pertama kali
    fragmentManager.beginTransaction().replace(R.id.main_container, new HomeFragment()).commit();

    //Memberikan listener saat menu item di bottom navigation diklik
    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        //Edit start    int id = item.getItemId();
            int layoutId = R.id.main_container;
            Bundle data = null;
            switch (id) {
                case R.id.Home:
                    fragment = new HomeFragment();
                    break;
                case R.id.Entri:
                    fragment = new EntriFragment();
                    break;
                case R.id.gudang:
                    fragment = new GudangFragment();
                    break;
                case R.id.Laporan:
                    fragment = new LaporanFragment();
                    break;
                case R.id.Profil:// in this line i try to passing the data
                    String nama1 = tvnama.getText().toString();
                    data = new Bundle();
                    data.putString(ProfileFragment.KEY_ACTIVITY, nama1);
                    fragment = new ProfileFragment();

                    layoutId = R.id.frame_profile;
                    break;
            }
            if (fragment != null) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (data != null) {
                    fragment.setArguments(data);
                }
                transaction.replace(layoutId, fragment).commit();
            }

            return true;
        }
    });
}//edit finish

ProfileFragment.java

    final String Nama = getActivity().getIntent().getStringExtra("TAG_NAMA");
    final String Jabatan = getActivity().getIntent().getStringExtra("TAG_JABATAN");
    final String Tlp = getActivity().getIntent().getStringExtra("TAG_TLP");

    sharedPreferences = getActivity().getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
    tvNama = getActivity().findViewById(R.id.tv_nama);
    tvJabatan = getActivity().findViewById(R.id.tv_jabatan);
    tvTlp = getActivity().findViewById(R.id.tv_tlp);

    try {
        String nama1 = getArguments().getString(KEY_ACTIVITY);
        if (nama1 != null) {
            tvNama.setText(nama1);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }`this is right?`


}

profile_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/frame_profile"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.ProfileFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/colorgradient"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                tools:ignore="UselessParent"
                tools:targetApi="o">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    tools:ignore="UselessParent">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:layout_gravity="center"
                        android:layout_marginTop="30dp"
                        android:layout_marginBottom="30dp"
                        android:src="@drawable/profile"
                        app:civ_border_color="#000"
                        app:civ_border_width="2dp" />


                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="16dp"
            android:layout_marginVertical="16dp"
            android:orientation="vertical"
            tools:targetApi="o">

            <TextView
                android:id="@+id/tv_nama"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/javatext"
                android:text="nama"
                android:textSize="30sp"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginBottom="30dp"
                android:background="#000" />


            <TextView
                android:id="@+id/tv_jabatan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/javatext"
                android:text="@string/jabatan"
                android:textSize="30sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_tlp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/javatext"
                android:text="@string/no_hp"
                android:textSize="30sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>


</FrameLayout>

这是错误//编辑

   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
    at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)
    at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444)
    at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:434)
    at com.example.iventorypurilupin.MainActivity$1.onNavigationItemSelected(MainActivity.java:91)
    at android.support.design.widget.BottomNavigationView$1.onMenuItemSelected(BottomNavigationView.java:204)
    at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:840)
    at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
    at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:991)
    at android.support.design.internal.BottomNavigationMenuView$1.onClick(BottomNavigationMenuView.java:115)
    at android.view.View.performClick(View.java:6913)
    at android.view.View.performClickInternal(View.java:6890)
    at android.view.View.access$3200(View.java:792)
    at android.view.View$PerformClick.run(View.java:27158)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:209)
    at android.app.ActivityThread.main(ActivityThread.java:7021)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:486)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)
2019-06-20 17:26:19.927 12942-12942/com.example.iventorypurilupin I/Process: Sending signal. PID: 12942 SIG: 9

2 个答案:

答案 0 :(得分:0)

尝试最后添加此支票

if(id != R.id.profil){
     final FragmentTransaction transaction = fragmentManager.beginTransaction();
     transaction.replace(R.id.main_container, fragment).commit();
}
return true;

答案 1 :(得分:0)

您选择的导航项回调应如下所示:

   public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        int layoutId = R.id.main_container;
        Bundle data = null;
        switch (id) {
            case R.id.Home:
                fragment = new HomeFragment();
                break;
            case R.id.Entri:
                fragment = new EntriFragment();
                break;
            case R.id.gudang:
                fragment = new GudangFragment();
                break;
            case R.id.Laporan:
                fragment = new LaporanFragment();
                break;
            case R.id.Profil:// in this line i try to passing the data
                String nama1=tvnama.getText().toString();
                data =new Bundle();
                data.putString(ProfileFragment.KEY_ACTIVITY,nama1);
                fragment = new ProfileFragment();

                layoutId = R.id.frame_profile;
                break;
        }
         if(fragment != null) {
              FragmentTransaction transaction = fragmentManager.beginTransaction();
              if(data!= null) {
                  fragment.setArguments(data);
              }
              transaction.replace(layoutId , fragment).commit();
          }

        return true;
    }