1

我正在尝试将 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 时显示。

谢谢!

4

1 回答 1

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());

所以我在我的视图上膨胀了我的布局(content_acoes_musculares),创建了我使用的小部件,并使用我膨胀的视图创建了我的 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);
}

我希望这会有所帮助。

谢谢。

于 2013-10-01T14:14:45.647 回答