Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NullPointerException onBackpressed(片段到活动)Android_Android_Android Intent_Android Fragments_Nullpointerexception_Android Actionbar - Fatal编程技术网

NullPointerException onBackpressed(片段到活动)Android

NullPointerException onBackpressed(片段到活动)Android,android,android-intent,android-fragments,nullpointerexception,android-actionbar,Android,Android Intent,Android Fragments,Nullpointerexception,Android Actionbar,每次使用actionbar上的后退按钮,从RouteView活动(使用片段)到RouteCose活动,我都会得到一个NullPointerException。问题是(我在这里得到的是null指针)OnCreate的routechose的以下值: citySave = b.getInt("stadt"); 如果我使用actionbar(父活动)上的后退按钮,我不知道为什么会得到空指针,但如果我使用手机上的后退按钮,我不会得到空指针。如果我使用actionbar菜单项point,我也不会得到空指针

每次使用actionbar上的后退按钮,从RouteView活动(使用片段)到RouteCose活动,我都会得到一个NullPointerException。问题是(我在这里得到的是null指针)OnCreate的routechose的以下值:

citySave = b.getInt("stadt");
如果我使用actionbar(父活动)上的后退按钮,我不知道为什么会得到空指针,但如果我使用手机上的后退按钮,我不会得到空指针。如果我使用actionbar菜单项point,我也不会得到空指针。我该如何调用actionbar左侧的小返回箭头来发送意图

注:如果有人有提示,如何简化If-else条件以防止冗余代码,欢迎:)

常规糖:

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 savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Bundle b = getIntent().getExtras();
        citySave = b.getInt("stadt");
        cityTitle = b.getString("titel_stadt");
        savedInstanceState.putInt("stadt", citySave);
    }

    @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) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.route, 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.
        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);
        }
    }
}

在以下部分中,您未按自己的意图传递任何值:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_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);
            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);
    }
}
你必须这样做:

 Intent route = new Intent(RouteView.this, RouteChooseActivity.class);
 route .putExtra("stadt", citySave);
 startActivity(route);

我添加了logcat,但不得不删除一些代码,因为有很多迹象……你对backbutton的理解有误,backbutton是手机上的back按钮,home按钮是actionbar上的按钮,通常返回navegation,第一个你得到的控制覆盖hook on Press backbutton,第二个是OnOptionsItemSelected,以android.R.id.home为例,很难理解您的解释。谢谢,但示例代码是什么样子的-我在谷歌找到了一些东西,但直到现在它才起作用:谢谢,我添加了一个bundle:bundle b=new bundle();b、 putInt(“Statt”,citySave);在案例R.id.action_路线,但我已经得到一个空指针。是的。示例:你能用一个示例来添加你的答案的编辑吗,一个工作代码应该是什么样子的。谢谢,但是在这种情况下,putInt方法无法解决-我认为一直都需要一个包,不是吗?你在使用eclipse吗?什么都没有发生。如果你的意思是ctrl o,我会在AndroidStudio上获得一个新窗口:选择要覆盖的方法,但putint不可用。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_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);
            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);
    }
}
 Intent route = new Intent(RouteView.this, RouteChooseActivity.class);
 route .putExtra("stadt", citySave);
 startActivity(route);