0
메뉴가 포함 된 서랍 레이아웃이 있고 첫 번째 메뉴 항목을 내 mainActivity.java 파일의 기본 선택 항목으로 설정했습니다. 그런 다음, 이전에 체크 된 메뉴 항목을 체크 된 항목으로 제거하고 새롭게 선택된 메뉴 항목을 체크 된 항목으로 설정할 수 있도록 previousItem이라는 int 변수를 사용하여 일부 코드를 작성했습니다. 그러나, 나는 계속 IndexOutOfBounds Exception을 얻었고이 문제에 대한 해결책을 찾는 방법을 모른다.서랍 메뉴의 항목 확인 및 선택 취소
public class MainActivity extends AppCompatActivity {
int previousItem;
// Declaring our dialog
ImportantDialogFragment dialogFragment = new ImportantDialogFragment();
GridView gridView;
// NavMenu member vars
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
private ActionBarDrawerToggle mToggle; // Button for toggling the side menu
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Showing the disclaimer dialog every time the app starts
dialogFragment.show(getSupportFragmentManager(),"IMPORTANT_NOTICE");
// Get Screen Size Details
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
final int width = displayMetrics.widthPixels;
gridView = (GridView) findViewById(R.id.gridViewLayout);
gridView.setAdapter(new ImageAdapter(this)); // used to set the contents of the GridView-in this case images-
// When an item from the GridView gets clicked
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Create a new Intent...
Toast.makeText(MainActivity.this, "Position: " + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,CardViewActivity.class);
intent.putExtra("Card Index",position);
intent.putExtra("SCREEN_WIDTH",width);
startActivity(intent);
}
});
mToolbar = (Toolbar) findViewById(R.id.navActionBar);
setSupportActionBar(mToolbar); // check quick doq
getSupportActionBar().setDisplayShowTitleEnabled(false);
mToolbar.setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.drawer_open,R.string.drawer_closed); // Instantiating our button
navigationView = (NavigationView) findViewById(R.id.nav_view);
// Sets the default selected menu item, to the Home item
navigationView.getMenu().findItem(R.id.nav_home).setChecked(true);
previousItem = navigationView.getMenu().getItem(0).getItemId();
// When an item inside the NavView gets clicked, then handle the event...
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
mDrawerLayout.closeDrawer(Gravity.START);
break;
case R.id.nav_UserBoxGLB:
// USERBOX GLB ACTIVITY
Intent userBoxGLBintent = new Intent(MainActivity.this,UserBoxGLBActivity.class);
startActivity(userBoxGLBintent);
break;
case R.id.nav_UserBoxJP:
break;
case R.id.nav_settings:
break;
case R.id.nav_feedback:
break;
case R.id.nav_contact_us:
break;
case R.id.nav_donate:
// Open the website's URL in a browser window
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
break;
case R.id.nav_about:
break;
default:
return onNavigationItemSelected(item);
}
navigationView.getMenu().getItem(previousItem).setChecked(false);
item.setChecked(true);
previousItem = navigationView.getMenu().findItem(item.getItemId()).getItemId();
mDrawerLayout.closeDrawers();
return false;
}
});
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
}
// When an item from the Action Bar gets tapped, then...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
return onOptionsItemSelected(item);
}
}
예외 : 당신은 항목 ID 대신 메뉴 내의 항목의 위치를 사용하는
11-21 14:27:45.553 19074-19074/com.dcv.spdesigns.dokkancards E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dcv.spdesigns.dokkancards, PID: 19074
java.lang.IndexOutOfBoundsException: Index: 2131230840, Size: 5
at java.util.ArrayList.get(ArrayList.java:411)
at android.support.v7.view.menu.MenuBuilder.getItem(MenuBuilder.java:741)
at com.dcv.spdesigns.dokkancards.MainActivity$2.onNavigationItemSelected(MainActivity.java:122)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:154)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:342)
at android.view.View.performClick(View.java:6261)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
을, 그것은 '너무 짧고 아무튼입니다 많이 설명하지 마라. 무엇부터 시작해야할까요? –
onNavigationItemSelected() 에서 'previousItem = items.indexOf (menuItem)'를 사용하여 항목의 위치를 저장해야합니다. – dalla92
미안하지만 하단의 참조 링크를 보지 못했습니다. 그게 해결 됐어, 고마워! –