虚拟指针在向上导航android

时间:2014-08-09 15:12:15

标签: android android-intent bundle menuitem up-navigation

当点击动作栏上的向上导航时,我想通过意图发送两个额外内容。在声明清单父活动时,在OnCreate处设置第二个活动getActionBar().setDisplayHomeAsUpEnabled(true);。但父活动需要一个工作的意图值(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);
        }
    }
}

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.cityknight.app/de.cityknight.app.RouteChooseActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at de.cityknight.app.RouteChooseActivity.onCreate(RouteChooseActivity.java:44)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

在您的活动的onCreate()方法中,Bundle将具有以下值:(来自official documentation

  

如果在之前关闭之后重新初始化活动   然后这个Bundle包含它最近提供的数据   的onSaveInstanceState(束)。注意:否则为空。

getIntent().getExtras()的内容相同。所以你应该检查这个值以避免NullPointerException。

Bundle b = getIntent().getExtras();
if (b != null) {
  citySave = b.getInt("stadt");
  cityTitle = b.getString("titel_stadt");
}

答案 1 :(得分:0)

在StackOverflow上找到正确答案:https://stackoverflow.com/a/17342137/3762248

&#34;&#34;标准&#34; Android活动的行为是,每次有此活动的新意图时,都会创建活动的新实例(请参阅此处的launchMode-docu)。因此,如果你调用navigateUpTo,你的额外内容似乎就消失了。

在您的情况下,我建议使用

android:launchMode="singleTop"

用于AndroidManifest.xml中的父活动。这样您将返回到现有活动(只要它位于任务的后堆栈顶部)。这样你的演员就会被保留。

我也是,不明白为什么在你引用的Google文档中没有提到这一点,因为这似乎是使用向上导航时所期望的行为。&#34;