2015-05-11 79 views
0

我是Android Studio中的noob,現在我卡住了創建我的app.I在我的主要活動中創建了一個浮動操作按鈕並單擊它我想顯示一個對話框詢問用戶該做什麼。在這個特定的情況下,我有2個選項可供選擇,並點擊其中的每個選項,我想打開2個不同的活動,名爲AddProductNoEan和AddProductEan。我嘗試使用下面的代碼,但在獲得錯誤後關閉最後一個大括號在我的onCreate.I已經在其他應用程序中使用了相同的過程,它的工作,所以在嘗試了很多次以不同的方式沒有成功我完全卡住了!有沒有人有一些提示來解決這個問題或一個更好的方式來實現我的目標?提前致謝! 單擊按鈕後在對話框中啓動活動

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)); 
     } 
    } 
} 
+0

您應該爲您的問題添加兩個標籤:一個關於語言,另一個關於平臺。它會幫助其他人找到你的問題。 – Michas

+0

感謝您的提示Michas,我剛剛編輯添加相關標籤! – Davide

回答

0

你必須在}};

在結束onCreate()只是錯誤應該});這將解決您的錯誤。

相關問題