单击按钮后启动对话框上的活动

时间:2015-05-11 11:21:05

标签: java android button android-studio dialog

我在Android Studio中使用了noob,现在我无法创建我的app.I在我的主要活动中创建了一个浮动操作按钮并点击它我想要显示一个对话框要求用户该怎么办。在这种特殊情况下,我有2个选项可供选择,点击其中每个选项我想打开2个不同的活动,名为AddProductNoEan和AddProductEan。我尝试使用下面的代码,但是在得到错误之后在我的onCreate结束时关闭最后一个大括号。我已经在其他应用程序中使用了相同的程序并且它有效,所以在以不同的方式尝试多次而没有成功之后我完全陷入困境!是否有人提示要解决这个问题问题或更好的方法来实现我的目标?提前致谢!

public class Inventory extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {
    FloatingActionButton add;
    AlertDialog.Builder build;


    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory);
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
        ImageView icon = new ImageView(this); // Create an icon
        icon.setImageResource(R.drawable.ic_action_new);
        add = new FloatingActionButton.Builder(this)
                .setContentView(icon)
                .setPosition(4)
                .build();
        add.setOnClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                build = new AlertDialog.Builder(Inventory.this);
                build.setTitle(R.string.delete);
                build.setMessage(R.string.question_delete);
                build.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent noean = new Intent(Inventory.this, AddProductNoEan.class);
                        startActivity(noean);
                        dialog.cancel();

                    }
                });
                build.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent eans = new Intent(Inventory.this, AddProductEan.class);
                        startActivity(eans);
                        dialog.cancel();
                    }
                });
                AlertDialog alert = build.create();
                alert.show();
                return false;
            }
        }};


    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                Intent adne = new Intent(Inventory.this, AddProductNoEan.class);
                startActivity(adne);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                Intent lista1 = new Intent(Inventory.this, ListaProdottiNoEan.class);
                startActivity(lista1);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                Intent ade = new Intent(Inventory.this, AddProductEan.class);
                startActivity(ade);
                break;
            case 4:
                mTitle = getString(R.string.title_activity_lista_prodotti_ean);
                Intent lista2 = new Intent(Inventory.this, ListaProdottiEan.class);
                startActivity(lista2);
                break;
            case 5:
                mTitle = getString(R.string.title_activity_all__products);
                Intent ap = new Intent(Inventory.this, All_Products.class);
                startActivity(ap);
                break;
            case 6:
                mTitle = getString(R.string.title_activity_gallery);
                Intent ga = new Intent(Inventory.this, Gallery.class);
                startActivity(ga);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.inventory, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @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);
    }


    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_inventory, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((Inventory) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

onCreate()

结尾}};时出错

它应该是});它将解决您的错误。