我正在尝试将PopupWindow与适配器一起使用,但会发生此异常。 我试图找到解决这个问题的东西,但我没有找到任何东西。
这是例外
07-18 10:58:27.600: D/dalvikvm(15218): GC_FOR_ALLOC freed 56K, 3% free 6409K/6595K, paused 40ms
07-18 10:58:27.600: I/dalvikvm-heap(15218): Grow heap (frag case) to 6.804MB for 513744-byte allocation
07-18 10:58:27.669: D/dalvikvm(15218): GC_FOR_ALLOC freed 8K, 3% free 6902K/7111K, paused 41ms
07-18 10:58:27.749: D/dalvikvm(15218): GC_CONCURRENT freed <1K, 3% free 6902K/7111K, paused 3ms+12ms
07-18 10:58:29.619: D/post(15218): Result from post JsonPost : 200 : {"success":false}
07-18 10:58:29.809: D/dalvikvm(15218): GC_CONCURRENT freed 103K, 3% free 7235K/7431K, paused 3ms+2ms
07-18 10:58:30.459: D/dalvikvm(15218): GC_FOR_ALLOC freed 217K, 5% free 7333K/7687K, paused 99ms
07-18 10:58:30.499: I/dalvikvm-heap(15218): Grow heap (frag case) to 12.920MB for 5981168-byte allocation
07-18 10:58:30.569: D/dalvikvm(15218): GC_FOR_ALLOC freed <1K, 3% free 13174K/13575K, paused 35ms
07-18 10:58:30.649: D/dalvikvm(15218): GC_CONCURRENT freed <1K, 3% free 13174K/13575K, paused 3ms+2ms
07-18 10:58:31.499: D/dalvikvm(15218): GC_CONCURRENT freed 6137K, 42% free 9002K/15303K, paused 3ms+3ms
07-18 10:58:59.759: D/AndroidRuntime(15218): Shutting down VM
07-18 10:58:59.759: W/dalvikvm(15218): threadid=1: thread exiting with uncaught exception (group=0x40014760)
07-18 10:58:59.769: E/AndroidRuntime(15218): FATAL EXCEPTION: main
07-18 10:58:59.769: E/AndroidRuntime(15218): java.lang.NullPointerException
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.widget.PopupWindow.getMaxAvailableHeight(PopupWindow.java:1164)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1067)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.widget.ListPopupWindow.show(ListPopupWindow.java:517)
07-18 10:58:59.769: E/AndroidRuntime(15218): at br.com.timo.atlas.PrincipalActivity.onClick(PrincipalActivity.java:123)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.view.View.performClick(View.java:3110)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.view.View$PerformClick.run(View.java:11934)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.os.Handler.handleCallback(Handler.java:587)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.os.Looper.loop(Looper.java:132)
07-18 10:58:59.769: E/AndroidRuntime(15218): at android.app.ActivityThread.main(ActivityThread.java:4123)
07-18 10:58:59.769: E/AndroidRuntime(15218): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 10:58:59.769: E/AndroidRuntime(15218): at java.lang.reflect.Method.invoke(Method.java:491)
07-18 10:58:59.769: E/AndroidRuntime(15218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-18 10:58:59.769: E/AndroidRuntime(15218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-18 10:58:59.769: E/AndroidRuntime(15218): at dalvik.system.NativeStart.main(Native Method)
这是我的主要类代码
public class PrincipalActivity extends Activity implements OnClickListener{
private ListView lv_fragmentLista_Lista;
private ListView lv_fragmentLista_ListaDireita;
private ListPopupWindow listPopupWindow;
private View popupWindow;
private Button bt_activityTopBarMenu_AreaMusculares;
private Button bt_activityTopBarMenu_MeusFavoritos;
private Button bt_activityTopBarMenu_Dicionario;
private Button bt_activityInfosPrincipal_BotaoMais;
private Button bt_activityInfosPrincipal_BotaoMenos;
private GrupoMuscularAdapter gma;
private MusculoAdapter ma;
private PopupAdapter popupAdapter;
// private final Typeface tfCabinSemiBoldItalic = Typeface.createFromAsset(getAssets(), "fonts/Cabin-SemiBoldItalic.ttf");
// private final Typeface tfCabinMediumItalic = Typeface.createFromAsset(getAssets(), "fonts/Cabin-MediumItalic.ttf");
// private final Typeface tfCabinMedium = Typeface.createFromAsset(getAssets(), "fonts/Cabin-Medium.ttf");
// private final Typeface tfCabinItalic = Typeface.createFromAsset(getAssets(), "fonts/Cabin-Italic.ttf");
// private final Typeface tfCabinBoldItalic = Typeface.createFromAsset(getAssets(), "fonts/Cabin-BoldItalic.ttf");
// private final Typeface tfCabinBold = Typeface.createFromAsset(getAssets(), "fonts/Cabin-Bold.ttf");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
AtlasDataBaseHelper dbHelper = new AtlasDataBaseHelper(this);
bt_activityTopBarMenu_AreaMusculares = (Button) findViewById(R.id.bt_activityTopBarMenu_AreaMusculares);
bt_activityTopBarMenu_AreaMusculares.setOnClickListener(this);
bt_activityTopBarMenu_MeusFavoritos = (Button) findViewById(R.id.bt_activityTopBarMenu_MeusFavoritos);
bt_activityTopBarMenu_MeusFavoritos.setOnClickListener(this);
bt_activityTopBarMenu_Dicionario = (Button) findViewById(R.id.bt_activityTopBarMenu_Dicionario);
bt_activityTopBarMenu_Dicionario.setOnClickListener(this);
bt_activityInfosPrincipal_BotaoMais = (Button) findViewById(R.id.bt_activityInfosPrincipal_BotaoMais);
bt_activityInfosPrincipal_BotaoMais.setOnClickListener(this);
bt_activityInfosPrincipal_BotaoMenos = (Button) findViewById(R.id.bt_activityInfosPrincipal_BotaoMenos);
bt_activityInfosPrincipal_BotaoMenos.setOnClickListener(this);
List<AreaMuscular> lstAreaMuscular = dbHelper.selectAllAreaMuscular();
gma = new GrupoMuscularAdapter(this,lstAreaMuscular);
ma = new MusculoAdapter(this);
List<Dicionario> lstDicionario = dbHelper.selectAllDicionario();
popupAdapter = new PopupAdapter(this,lstDicionario);
popupWindow = findViewById(R.layout.fragment_lista);
listPopupWindow = new ListPopupWindow(this);
listPopupWindow.setAnchorView(popupWindow);
listPopupWindow.setContentWidth(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_FROM_FOCUSABLE);
listPopupWindow.setAdapter(popupAdapter);
lv_fragmentLista_ListaDireita = (ListView) findViewById(R.id.lv_activity_ListaMenuDireito);
lv_fragmentLista_ListaDireita.setAdapter(ma);
lv_fragmentLista_Lista = (ListView) findViewById(R.id.lv_activity_ListaMenuEsquerdo);
lv_fragmentLista_Lista.setAdapter(gma);
lv_fragmentLista_Lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mudaFundoBotao(0);
lv_fragmentLista_Lista.setVisibility(View.GONE);
lv_fragmentLista_ListaDireita.setVisibility(View.VISIBLE);
bt_activityTopBarMenu_AreaMusculares.setVisibility(View.VISIBLE);
Log.i("ID", id + "");
Log.i("POSITION", position + "");
}
});
setFontFace();
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.bt_activityTopBarMenu_AreaMusculares){
mudaFundoBotao(R.id.bt_activityTopBarMenu_AreaMusculares);
bt_activityTopBarMenu_AreaMusculares.setVisibility(View.GONE);
lv_fragmentLista_ListaDireita.setVisibility(View.GONE);
lv_fragmentLista_Lista.setVisibility(View.VISIBLE);
}
if(v.getId() == R.id.bt_activityTopBarMenu_MeusFavoritos){
mudaFundoBotao(R.id.bt_activityTopBarMenu_MeusFavoritos);
}
if(v.getId() == R.id.bt_activityTopBarMenu_Dicionario){
mudaFundoBotao(R.id.bt_activityTopBarMenu_Dicionario);
listPopupWindow.show();
}
if(v.getId() == R.id.bt_activityInfosPrincipal_BotaoMenos){
}
if(v.getId() == R.id.bt_activityInfosPrincipal_BotaoMais){
}
}
private void mudaFundoBotao(int idBotao){
if(idBotao == R.id.bt_activityTopBarMenu_AreaMusculares){
bt_activityTopBarMenu_AreaMusculares.setBackgroundResource(R.drawable.bt_ativo);
bt_activityTopBarMenu_MeusFavoritos.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_Dicionario.setBackgroundResource(R.drawable.bt_normal);
}
if(idBotao == R.id.bt_activityTopBarMenu_MeusFavoritos){
bt_activityTopBarMenu_AreaMusculares.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_MeusFavoritos.setBackgroundResource(R.drawable.bt_ativo);
bt_activityTopBarMenu_Dicionario.setBackgroundResource(R.drawable.bt_normal);
}
if(idBotao == R.id.bt_activityTopBarMenu_Dicionario){
bt_activityTopBarMenu_AreaMusculares.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_MeusFavoritos.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_Dicionario.setBackgroundResource(R.drawable.bt_ativo);
}
if(idBotao == 0){
bt_activityTopBarMenu_AreaMusculares.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_MeusFavoritos.setBackgroundResource(R.drawable.bt_normal);
bt_activityTopBarMenu_Dicionario.setBackgroundResource(R.drawable.bt_normal);
}
}
private void setFontFace(){
Typeface tfCabinRegular = Typeface.createFromAsset(getAssets(), "fonts/Cabin-Regular.ttf");
Typeface tfCabinSemiBold = Typeface.createFromAsset(getAssets(), "fonts/Cabin-SemiBold.ttf");
bt_activityTopBarMenu_AreaMusculares.setTypeface(tfCabinSemiBold);
bt_activityTopBarMenu_Dicionario.setTypeface(tfCabinRegular);
bt_activityTopBarMenu_MeusFavoritos.setTypeface(tfCabinRegular);
}
}
第123行是listPopupWindow.show();
我正在创建ListPopupWindow并尝试显示何时单击按钮R.id.bt_activityTopBarMenu_Dicionario。
谢谢!
答案 0 :(得分:1)
确定。 这不是这个错误的解决方案,但它是我找到的替代路线。
首先我创建了这个变量
private LayoutInflater popupWindowInflaterAcoesMusculares;
private View viewPopupWindowAcoesMusculares;
private PopupWindow popupWindowAcoesMusculares;
接下来我的onCreate方法使用此代码
popupWindowInflaterAcoesMusculares = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewPopupWindowAcoesMusculares = popupWindowInflaterAcoesMusculares.inflate(R.layout.content_acoes_musculares, null,true);
vp_contentAcoesMusculares_SequenciaExercicios = (ViewPager) viewPopupWindowAcoesMusculares.findViewById(R.id.vp_contentAcoesMusculares_SequenciaExercicios);
tv_contentAcoesMusculares_NomeSequencia = (TextView) viewPopupWindowAcoesMusculares.findViewById(R.id.tv_contentAcoesMusculares_NomeSequencia);
tv_contentAcoesMusculares_DescricaoSequencia = (TextView) viewPopupWindowAcoesMusculares.findViewById(R.id.tv_contentAcoesMusculares_DescricaoSequencia);
popupWindowAcoesMusculares = new PopupWindow(viewPopupWindowAcoesMusculares,400,500,true);
popupWindowAcoesMusculares.setOutsideTouchable(true);
popupWindowAcoesMusculares.setBackgroundDrawable(new BitmapDrawable());
所以我在我的View上夸大了我的布局(content_acoes_musculares),创建了我使用的小部件,并使用View I inflated创建了我的PopupWindow。
为了显示我的PopupWindow,我在下面使用了这段代码。
if(!popupWindowAcoesMusculares.isShowing()){
int[] local = new int[2];
bt_activityInfosPrincipal_AcoesMusculares.getLocationOnScreen(local);
popupWindowAcoesMusculares.showAtLocation(v,Gravity.NO_GRAVITY,local[0],local[1] + 60);
}
我希望这有帮助。
感谢。