I'm trying to send data from MainActivity to TimeActivity but the bundle received in TimeActivity is null. And a Null Pointer Exception is thrown when I do not put
s = extras.getString("key");
in the
if (extras != null) {
}
and a message saying "The App has unfortunately stopped". but after pressing OK to the message, the TimeActivity starts and the value 125 is received in
String s;
This is MainActivity.
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedpreferences;
TextView name;
TextView email;
public static final String mypreference = "mypref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView) findViewById(R.id.etName);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String n = name.getText().toString();
String e = "1";
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, n);
editor.commit();
if(n.length()==3){
Intent intent = new Intent(MainActivity.this, TimeActivity.class);
startActivity(intent);}else{
Toast.makeText(MainActivity.this,"The Roll Number Must be a 3 Digit number",Toast.LENGTH_LONG).show();
}
}
}
);
sharedpreferences=
getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
if(sharedpreferences.contains(Name))
{
String s = sharedpreferences.getString(Name, "");
Intent i = new Intent(getApplicationContext(), TimeActivity.class);
i.putExtra("key", s);
startActivity(i);
}
else
{
}
}
}
This is TimeActivity
public class TimeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Timely");setSupportActionBar(toolbar);
String s="0";
Bundle extras = getIntent().getExtras();
if (extras != null) {
Intent i = new Intent(getApplicationContext(), TimeActivity.class);
startActivity(i);s = extras.getString("key");
}else{Toast.makeText(TimeActivity.this,"Bundle received Null. ",Toast.LENGTH_LONG).show();}
int r = Integer.parseInt(s);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
System.exit(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.time, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
} else if (id == R.id.nav_timetable) {
} else if (id == R.id.nav_about) {
startActivity(new Intent(TimeActivity.this, About.class));
} else if (id == R.id.nav_Develop) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Any kind of help will be appreciated.