指向Upnavigation android的空指针
当点击actionbar上的upnavigation时,我想通过intent发送两个额外信息。在清单上声明父活动,在第二个活动上创建getActionBar.SetDisplayHomeAsupanabledTrue;一切就绪。但是父活动需要一个intent值才能使用citySave。我已经尝试了很多,但是我没有成功,upnavigation将意向值发送到父活动R.id.home 我甚至没有添加第二个活动中的全部代码,因为这会有太多的符号。我在第二个活动中使用了静态定义的片段2。我不知道,这是否与问题有关 第二项活动:指向Upnavigation android的空指针,android,android-intent,bundle,menuitem,up-navigation,Android,Android Intent,Bundle,Menuitem,Up Navigation,当点击actionbar上的upnavigation时,我想通过intent发送两个额外信息。在清单上声明父活动,在第二个活动上创建getActionBar.SetDisplayHomeAsupanabledTrue;一切就绪。但是父活动需要一个intent值才能使用citySave。我已经尝试了很多,但是我没有成功,upnavigation将意向值发送到父活动R.id.home 我甚至没有添加第二个活动中的全部代码,因为这会有太多的符号。我在第二个活动中使用了静态定义的片段2。我不知道,这是否
public class RouteView extends ActionBarActivity {
Integer citySave;
String cityTitle;
Integer routeSave;
String routeTitel;
private static int NUM_ITEMS = 2;
SectionsPagerAdapter mSectionsPagerAdapter;
ListView listView;
ArrayList<RowItem> pois;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_view);
Bundle b = getIntent().getExtras();
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
routeSave = b.getInt("route");
routeTitel = b.getString("titel_route");
b.putString("titel_route",routeTitel);
b.putInt("route", routeSave);
getActionBar().setDisplayHomeAsUpEnabled(true);
//RouteItemFragment fragobj = new RouteItemFragment();
//fragobj.setArguments(b);
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
setTitle("Route:" + " " + routeTitel);
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.setClass(RouteView.this, RouteChooseActivity.class);
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
intent.putExtras(b);
startActivity(intent);
finish();
}
public void onPause() {
super.onPause();
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putInt("route", routeSave);
b.putString("route_titel",routeTitel);
finish();
}
public void onResume() {
super.onResume();
Bundle b = getIntent().getExtras();
citySave = b.getInt("stadt");
routeSave = b.getInt("route");
routeTitel = b.getString("route_titel");
}
public void onStop() {
super.onStop();
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
NavUtils.navigateUpFromSameTask(this);
Intent upIntent = NavUtils.getParentActivityIntent(this);
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
upIntent.putExtras(b);
//startActivity(upIntent);
return true;
case R.id.action_stadt:
setContentView(R.layout.activity_stadt);
Intent stadt = new Intent(RouteView.this, StadtActivity.class);
startActivity(stadt);
return true;
case R.id.action_route:
setContentView(R.layout.activity_route_choose);
Intent route = new Intent(RouteView.this, RouteChooseActivity.class);
route.putExtra("stadt", citySave);
route.putExtra("titel_stadt", cityTitle);
startActivity(route);
return true;
case R.id.action_help:
setContentView(R.layout.activity_help);
Intent help = new Intent(RouteView.this, Help.class);
startActivity(help);
return true;
case R.id.action_exit:
moveTaskToBack(true);
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
public class RouteChooseActivity extends ActionBarActivity implements OnItemClickListener {
Integer citySave;
Integer routeSave;
String cityTitle;
ListView listView;
List<RowItem> cities;
CityParser parser = new CityParser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_choose);
Bundle b = getIntent().getExtras();
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
TextView headingRoute = (TextView) findViewById(R.id.routeTitel);
headingRoute.setText(String.format(getResources().getString(R.string.route_text)) + " " + cityTitle + " aus:");
listView = (ListView) findViewById(R.id.listRoute);
if (citySave.equals(1)) {
try {
cities = parser.parse(getAssets().open("route_passau.xml"));
CityListViewAdapter adapter = new CityListViewAdapter(this, R.layout.row_items, (ArrayList<RowItem>) cities);
Collections.sort(cities, new Comparator<RowItem>() {
public int compare(RowItem s1, RowItem s2) {
return s1.getTitle().compareTo(s2.getTitle());
}
});
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
} else if (citySave.equals(2)) {
try {
cities = parser.parse(getAssets().open("route_bamberg.xml"));
CityListViewAdapter adapter = new CityListViewAdapter(this, R.layout.row_items, (ArrayList<RowItem>) cities);
Collections.sort(cities, new Comparator<RowItem>() {
public int compare(RowItem s1, RowItem s2) {
return s1.getTitle().compareTo(s2.getTitle());
}
});
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
} else if (citySave.equals(3)) {
try {
cities = parser.parse(getAssets().open("route_augsburg.xml"));
CityListViewAdapter adapter = new CityListViewAdapter(this, R.layout.row_items, (ArrayList<RowItem>) cities);
Collections.sort(cities, new Comparator<RowItem>() {
public int compare(RowItem s1, RowItem s2) {
return s1.getTitle().compareTo(s2.getTitle());
}
});
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
TextView errorMessage = (TextView) findViewById(R.id.routeTitel);
errorMessage.setText(R.string.errorroute);
} catch (NullPointerException e) {
Context context = getApplicationContext();
CharSequence text = getResources().getString(R.string.errorroute);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
listView.setOnItemClickListener(this);
final ActionBar actionBar = getSupportActionBar();
setTitle("Routen in" + " " + cityTitle);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(RouteChooseActivity.this, RouteView.class);
RowItem cities = (RowItem) parent.getItemAtPosition(position);
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
b.putInt("route", cities.getID());
b.putString("titel_route", cities.getTitle());
intent.putExtras(b);
startActivity(intent);
}
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("stadt", citySave);
outState.getString("titel_stadt", cityTitle);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
citySave = savedInstanceState.getInt("stadt");
Log.i("debug", "saved data: " + citySave);
}
@Override
public void onResume() {
super.onResume();
Bundle b = getIntent().getExtras();
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
}
@Override
public void onPause() {
super.onPause();
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
unbindDrawables(findViewById(R.id.Bild));
System.gc();
}
@Override
public void onStop() {
super.onStop();
Bundle b = new Bundle();
b.putInt("stadt", citySave);
b.putString("titel_stadt", cityTitle);
finish();
}
@Override
protected void onDestroy()
{
super.onDestroy();
unbindDrawables(findViewById(R.id.Bild));
System.gc();
}
@Override
public void onRestart() {
super.onRestart();
Bundle b = getIntent().getExtras();
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
}
private void unbindDrawables(View view)
{
if (view.getBackground() != null)
{
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView))
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.route, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stadt:
setContentView(R.layout.activity_stadt);
Intent stadt = new Intent(RouteChooseActivity.this, StadtActivity.class);
startActivity(stadt);
return true;
case R.id.action_help:
setContentView(R.layout.activity_help);
Intent help = new Intent(RouteChooseActivity.this, Help.class);
startActivity(help);
return true;
case R.id.action_exit:
moveTaskToBack(true);
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
在活动的onCreate方法中,Bundle将具有以下值:from
如果活动在先前关闭后重新初始化
然后,此捆绑包包含它最近提供的数据
onSaveInstanceStateBundle。注意:否则为空
你的getIntent.getExtras也一样。因此,您应该检查此值以避免NullPointerException
Bundle b = getIntent().getExtras();
if (b != null) {
citySave = b.getInt("stadt");
cityTitle = b.getString("titel_stadt");
}
在StackOverflow上找到正确答案: android活动的标准行为是,每次有新的活动意图时,都会创建一个新的活动实例,请参见此处的launchMode docu。因此,如果你打电话给navigateUpTo,你的临时演员似乎就不见了 在你的情况下,我建议使用
android:launchMode="singleTop"
用于AndroidManifest.xml中的父活动。这样,您将返回到现有活动,只要它位于任务后堆栈的顶部。这样你的多余部分就会被保留下来
我也不明白为什么你引用的Google文档中没有提到这一点,因为这似乎是使用完导航后人们所期望的行为。检查Bundle b是否为空。它不能为空,因为OnBackPressed正在工作。检查此项并告诉我发生了什么事RouteCoseActivity的第44行出现了NullPointerException,代码的其余部分在哪里?我已经添加了整个代码,但是第44行是citySave=b.getIntstadt;谢谢,我会尽快试一试。如果我理解正确,我必须设置为RestoreInstanceState?编辑答案。您必须检查该值是否为空。谢谢,else条件是什么?我已经测试过:else{citySave=savedInstanceState.getIntstadt;cityTitle=savedInstanceState.getStringtitel_stadt;}但是空指针。如果citySave.equals上没有else条件NullPointer…如果Bundle为null,则表示没有数据。因此,不要试图处理数据,因为会出现NullPointerException。正如你所看到的,我是一个该死的初学者。好的,我明白了。我怎样才能得到数据?
android:launchMode="singleTop"