2014-11-02 45 views
2

的PopupMenu()我使用自定義適配器,並添加上getView這個代碼()方法不工作

final ImageView popupMenu = (ImageView) v.findViewById(R.id.popupMenu); 
    popupMenu.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      popupMenu.setImageResource(R.drawable.ic_popup_menu_selected); 
      PopupMenu popup = new PopupMenu(context, view); 
      popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); 
      popup.setOnDismissListener(new PopupMenu.OnDismissListener() { 
       @Override 
       public void onDismiss(PopupMenu pm) { 
        popupMenu.setImageResource(R.drawable.ic_popup_menu); 
       } 
      }); 
      popup.show(); 
     } 
}); 

但我得到這個錯誤: java.lang.RuntimeException: Failed to resolve attribute at index 6

相同的代碼工作onListItemClick(),但它沒有意義,因爲它需要點擊一次設置,再次點擊觸發PopupMenu。

編輯:logcat的

11-02 17:58:51.276 1996-1996/com.android.example E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.android.example, PID: 1996 
java.lang.RuntimeException: Failed to resolve attribute at index 6 
     at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603) 
     at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6423) 
     at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:6591) 
     at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java:735) 
     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:679) 
     at android.widget.FrameLayout.generateLayoutParams(FrameLayout.java:62) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
     at android.support.v7.internal.view.menu.MenuPopupHelper$MenuAdapter.getView(MenuPopupHelper.java:363) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.measureContentWidth(MenuPopupHelper.java:212) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:146) 
     at android.support.v7.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:118) 
     at android.support.v7.widget.PopupMenu.show(PopupMenu.java:168) 
     at com.android.example.GetAdapter$listAdapter$1.onClick(GetAdapter.java:81) 
     at android.view.View.performClick(View.java:4756) 
     at android.view.View$PerformClick.run(View.java:19749) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
+0

你能發佈logcat錯誤? – 2014-11-02 19:03:06

+0

@GoranHoriaMihail完成。 – Shixons 2014-11-03 00:52:39

+0

GetAdapter.java:81這行是它崩潰的地方。這條線是什麼? – 2014-11-03 07:12:08

回答

0

我解決這個問題是這樣的:

添加這個適配器上的getView():

ImageView popupMenu = (ImageView) v.findViewById(R.id.popupMenu); popupMenu.setTag(getItem(position)); popupMenu.setOnClickListener(MyFragment.popupMenuListener);

這對MyFragment:

implements OnClickListener

popupMenuListener = this;

@Override public void onClick(final View view) { view.post(new Runnable() { @Override public void run() { showPopupMenu(view); } }); }

0

這個問題是與缺少主題屬性。
確保您的應用主題延伸至Theme.AppCompat

<style name="AppTheme" parent="@style/Theme.AppCompat" /> 
5

有以下進口我總是有錯誤:

import android.support.v7.widget.PopupMenu; 

它正常工作與以下導入:

import android.widget.PopupMenu; 

完整的答案:stackoverflow.com/a/27826670/4548500

+0

雖然這在理論上可以回答這個問題,但[這將是更可取的](// meta.stackoverflow.com/q/8259)在這裏包含答案的基本部分,並提供供參考的鏈接。 – 2016-02-23 17:02:08

+1

希望這能解決它。 – SUPERCILEX 2016-02-23 17:05:49

+0

完美,從myside Upvote修復 – 2016-02-23 17:06:57

相關問題