2013-07-18 55 views
1

我想用適配器使用PopupWindow,但發生此異常。 我試圖尋找解決這個問題的東西,但我沒有找到任何東西。NullPointer.Exception PopupWindow.getMaxAvailableHeight using ListPopupWindow

這裏是異常

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

哪行在你的文件中是123? – TalDroid

+0

@TalDroid - listPopupWindow.show(); – groff07

+0

嘿,我有同樣的問題。你有沒有找到解決方案?而就我而言,我不知道要重現這個錯誤。它只是隨我的客戶發生,我看到了ACRA日誌。 – Tulio

回答

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)在我看來,創建了我用的小部件並使用View I膨脹創建了我的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); 
} 

我希望能對您有所幫助。

謝謝。

+1

感謝您發佈您的解決方法!我認爲我的問題原因與你的不同,但你給了我一個最終幫助我的想法。 – Tulio