我尝试将数据从MainActivity发送到TimeActivity,但TimeActivity中收到的包是null。当我不放
时抛出空指针异常s = extras.getString("key");
中的
if (extras != null) {
}
和一条消息说"该应用程序不幸停止"。 但是在按OK之后,TimeActivity开始,并在
中接收值125String s;
这是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
{
}
}
}
这是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;
}
}
任何形式的帮助都将受到赞赏。
答案 0 :(得分:0)
我可以在这里看到两个可能的问题。
首先,您没有通过MainActivity
Intent intent = new Intent(MainActivity.this, TimeActivity.class);
startActivity(intent);
其次,您在TimeActivity
方法中再次启动onCreate()
。
if (extras != null) {
Intent i = new Intent(getApplicationContext(), TimeActivity.class);
startActivity(i);
s = extras.getString("key");
}