-2

I'm new to android. I use Shared Preferences named "MyPref" to store and retrieve values. The code logic is, "MyPref" stores userid, usertype (customer, driver). In my SplashActivity I have written the following code. If user id is not null and user type equals either customer or driver the activity should navigate accordingly. But I get the following error: Kindly help

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference

SplashActivity:

public class SplashActivity extends AppCompatActivity {
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context context;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        pref = context.getSharedPreferences("MyPref", 0);
        editor = pref.edit();
        String userid=pref.getString("userid","");
        Log.e("userid",userid);
        if(userid!=null)
        {
            String usertype=pref.getString("usertype1","");
            if(usertype.equals("customer"))
            {
                Intent customer=new Intent(SplashActivity.this, HomeFragment.class);
                startActivity(customer);
            }
            else
            {
                Intent driver=new Intent(SplashActivity.this, DriverDashboardFragment.class);
                startActivity(driver);
            }
        }
        else
        {
            Intent walk=new Intent(SplashActivity.this,WalkThroughActivity.class);
            startActivity(walk);
        }

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

            }
        }, 2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Elackya
  • 119
  • 1
  • 13

2 Answers2

3

Remove context from this statement:

pref = context.getSharedPreferences("MyPref", 0);

it should be pref = getSharedPreferences("MyPref", 0);

You are facing this issue because context is not initialized. You don't need context reference here because AppCompatActivity is child of Context class. So you can directly call getSharedPreferences.

After this your app is crashing because you are try to start a fragment via startActivity method. To start a fragment inside an activity:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,new DriverDashboardFragment(),"driverDashboard");
transaction.addToBackStack(null);
transaction.commit();

replace above code with:

Intent driver=new Intent(SplashActivity.this, DriverDashboardFragment.class);
                startActivity(driver);

You need to add this in your xml of this activity:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Suraj Vaishnav
  • 7,777
  • 4
  • 43
  • 46
1

Your context is null

just add this line after setcontentView

 context= this;

it will not crash..

Intsab Haider
  • 3,491
  • 3
  • 23
  • 32