2014-05-21 8 views
0

오류가 발생했습니다. 재생하는 데 문제가 있습니다. 내 생각에 애플 리케이션이 사용 중일 때 사용자가 aps를 전환하고 잠시 홈 화면으로 간다. 그런 다음 내 앱으로 돌아온다. 전체 오류는 다음과 같습니다android.view.InflateException 왜 강제 종료가 확실하지 않은가

android.view.InflateException: Couldn't resolve menu item onClick handler 
    goToSearch in class android.view.ContextThemeWrapper 

     at android.view.MenuInflater$InflatedOnMenuItemClickListener 
      .<init>(MenuInflater.java:202) 
     at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:402) 
     at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436) 
     at android.view.MenuInflater.parseMenu(MenuInflater.java:173) 
     at android.view.MenuInflater.inflate(MenuInflater.java:95) 
     at com.beerportfolio.beerportfoliopro.MainDrawer2.onCreateOptionsMenu(MainDrawer2.java:172) 
     at android.app.Activity.onCreatePanelMenu(Activity.java:2513) 
     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224) 
     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:415) 
     at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:770) 
     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3179) 
     at android.os.Handler.handleCallback(Handler.java:608) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:156) 
     at android.app.ActivityThread.main(ActivityThread.java:5060) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

Caused by: java.lang.NoSuchMethodException: 
    goToSearch [interface android.view.MenuItem] 

     at java.lang.Class.getConstructorOrMethod(Class.java:460) 
     at java.lang.Class.getMethod(Class.java:915) 
     at android.view.MenuInflater$InflatedOnMenuItemClickListener.<init>(MenuInflater.java:200) 
     at android.view.MenuInflater$MenuState.setItem(MenuInflater.java:402) 
     at android.view.MenuInflater$MenuState.addItem(MenuInflater.java:436) 
     at android.view.MenuInflater.parseMenu(MenuInflater.java:173) 
     at android.view.MenuInflater.inflate(MenuInflater.java:95) 
     at com.beerportfolio.beerportfoliopro.MainDrawer2.onCreateOptionsMenu(MainDrawer2.java:172) 
     at android.app.Activity.onCreatePanelMenu(Activity.java:2513) 
     at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224) 
     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:415) 
     at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:770) 
     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:3179) 
     at android.os.Handler.handleCallback(Handler.java:608) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:156) 
     at android.app.ActivityThread.main(ActivityThread.java:5060) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

내 주요 drawer2 클래스는 다음과 같습니다

public class MainDrawer2 extends FragmentActivity 
{ 
    private static final String EXTRA_NAV_ITEM = "extraNavItem"; 
    private static final String STATE_CURRENT_NAV = "stateCurrentNav"; 

    private ActionBarDrawerToggle mDrawerToggle; 
    private DrawerLayout mDrawerLayout; 

    private NavDrawerListAdapter mDrawerAdapter; 
    private ListView mDrawerList; 

    private CharSequence mTitle; 
    private CharSequence mDrawerTitle; 

    private MainNavItem mCurrentNavItem; 


    public static Intent createLaunchFragmentIntent(
     Context context, MainNavItem navItem) 
    { 
     return new Intent(context, MainDrawer2.class) 
       .putExtra(EXTRA_NAV_ITEM, navItem.ordinal()); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_main); 
     Crashlytics.start(this); 
     mTitle = mDrawerTitle = getTitle(); 
     mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView)findViewById(R.id.drawer); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     enableHomeButtonIfRequired(); 

     mDrawerAdapter = new NavDrawerListAdapter(getApplicationContext()); 
     mDrawerList.setAdapter(mDrawerAdapter); 
     mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, 
       View view, int position, long id) 
      { 
       displayNavFragment((MainNavItem)parent 
        .getItemAtPosition(position)); 
      } 
     }); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       R.drawable.ic_drawer, R.string.app_name, R.string.app_name) 
     { 
      public void onDrawerClosed(View view) 
      { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View drawerView) 
      { 
       getActionBar().setTitle(mDrawerTitle); 
       invalidateOptionsMenu(); 
      } 
     }; 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if(getIntent().hasExtra(EXTRA_NAV_ITEM)){ 
      MainNavItem navItem = MainNavItem.values() 
        [getIntent().getIntExtra(EXTRA_NAV_ITEM, 
        MainNavItem.STATISTICS.ordinal())]; 
      displayNavFragment(navItem); 
     } 
     else if(savedInstanceState != null){ 
      mCurrentNavItem = MainNavItem.values() 
        [savedInstanceState.getInt(STATE_CURRENT_NAV)]; 
      setCurrentNavItem(mCurrentNavItem); 
     } 
     else{ 
      displayNavFragment(MainNavItem.STATISTICS); 
     } 
    } 

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 
    private void enableHomeButtonIfRequired() 
    { 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){ 
      getActionBar().setHomeButtonEnabled(true); 
     } 
    } 
    public void setActionBarTitle(String title) { 
     getActionBar().setTitle(title); 
    } 

    @Override 
    public void setTitle(CharSequence title) 
    { 
     mTitle = title; 
     getActionBar().setTitle(mTitle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) 
    { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) 
    { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggles 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) 
    { 
     super.onSaveInstanceState(outState); 

     if (mCurrentNavItem == null) { 

     } 
     else{ 
      outState.putInt(STATE_CURRENT_NAV, mCurrentNavItem.ordinal()); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    /* 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) 
    { 
     // if nav drawer is opened, hide the action items 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     menu.findItem(R.id.action_settings).setVisible(!drawerOpen); 
     return super.onPrepareOptionsMenu(menu); 
    } 
    */ 

    private void displayNavFragment(MainNavItem navItem) 
    { 
     //if(navItem == mCurrentNavItem) { 
      // return; 
     //} 
     Fragment fragment = Fragment.instantiate(this, 
       navItem.getFragClass().getName()); 
     if(fragment != null) { 
      getSupportFragmentManager().beginTransaction() 
        .replace(R.id.main, fragment) 
        .commit(); 
      setCurrentNavItem(navItem); 
     } 
    } 

    private void setCurrentNavItem(MainNavItem navItem) 
    { 
     int position = navItem.ordinal(); 
     // If navItem is in DrawerAdapter 
     if(position >= 0 && position < mDrawerAdapter.getCount()){ 
      //mDrawerList.setItemChecked(position, true); 
     } 
     else { 
      // navItem not in DrawerAdapter, de-select current item 
      if(mCurrentNavItem != null){ 
       //mDrawerList.setItemChecked(mCurrentNavItem.ordinal(), false); 
      } 
     } 

     //test to keep item not selected 
     int toClear=mDrawerList.getCheckedItemPosition(); 

     if (toClear >= 0) { 
      mDrawerList.setItemChecked(toClear, false); 
     } 

     mDrawerLayout.closeDrawer(mDrawerList); 
     setTitle(navItem.getTitleResId()); 
     mCurrentNavItem = navItem; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case android.R.id.home: 
       if(mDrawerLayout.isDrawerOpen(mDrawerList)) { 
        mDrawerLayout.closeDrawer(mDrawerList); 
       } 
       else { 
        mDrawerLayout.openDrawer(mDrawerList); 
       } 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public void goToSearch(MenuItem item) { 
     //go to search page 
     Fragment Fragment_one; 
     FragmentManager man= getSupportFragmentManager(); 
     FragmentTransaction tran = man.beginTransaction(); 
     Fragment_one = new Search(); 

     tran.replace(R.id.main, Fragment_one);//tran. 
     tran.addToBackStack(null); 
     tran.commit(); 

    } 

    public void scanBarcode(MenuItem item) { 
     //open scanner 
     IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
     scanIntegrator.initiateScan(); 
    } 

    public void onActivityResult(int requestCode, 
     int resultCode, Intent intent) { 
     //retrieve scan result 
     IntentResult scanningResult = IntentIntegrator 
      .parseActivityResult(requestCode, resultCode, intent); 
     if (scanningResult != null) { 
      //we have a result 

      String scanContent = scanningResult.getContents(); 

      //todo: set scan content into setting, 
      //load new fragment which calls async task below. New 
      //todo: fragment will have same ui as search. :-) 
      Fragment Fragment_one; 
      FragmentManager man= this.getSupportFragmentManager(); 
      FragmentTransaction tran = man.beginTransaction(); 
      Fragment_one = new BarcodeFrag(scanContent); 
      tran.replace(R.id.main, Fragment_one);//tran. 
      tran.addToBackStack(null); 
      //tran.commit(); 
      tran.commitAllowingStateLoss(); 
     }  
     else { 
      Toast toast = Toast.makeText(getApplicationContext(), 
        "No scan data received!", Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 


} 

는 마지막으로 내 메뉴 XML은 다음과 같습니다

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity" > 

    <item android:id="@+id/menu_search2" 
     android:actionViewClass="android.widget.SearchView" 
     android:title="Search" 
     android:icon="@android:drawable/ic_menu_search" 
     android:showAsAction="always|collapseActionView" 
     android:onClick="goToSearch" /> 

    <item android:id="@+id/action_scan" 
     android:icon="@drawable/barcode" 
     android:onClick="scanBarcode" 
     android:showAsAction="ifRoom|collapseActionView"/> 
</menu> 

내가 할 수없는 내 인생에 그림에 대한 이 오류의 출처

+1

타겟팅 무엇 API 참조 http://developer.android.com/reference/android/R.attr.html#onClick 를 참조하십시오 어떻게해야입니다 이것은 당신

public void goToSearch(MenuItem item){ } 

입니까? 다음과 유사하게 보입니다. http://stackoverflow.com/questions/11245829/inflateexception-couldnt-resolve-menu-item-onclick-handler –

+0

Mike

답변

2

수동으로 클릭 방법을 연결하는 대신 가능한 경우 onOptionsItemSelected 메커니즘을 사용하는 것이 좋습니다. 당신은 플랫폼 버그를 발견했을 수도 Android Developer Guide:Menus

+0

지금 시도해보십시오. – Mike

+0

두 파일을 모두 다음으로 변경했습니다. https://gist.github.com/ananate/83b24469a0f8cdeaf52b 이제 파일을 열 때마다 강제로 닫힙니다. – Mike

0

:

는 문서를 살펴 보자. 당신이 당신의 활동 테마를 Theme로 설정하면 그것은 여전히 ​​발생합니까? Holo 또는 Theme.Holo.Light? 특히 Theme.Holo.Light.DarkActionBar를 기반으로하지 않는 무언가로 시도하십시오.

원래 문제가 해결되면 http://b.android.com으로 알려주십시오.

0

잘못된 서명이 있습니다.

이는

public void goToSearch(View v){ 
}