2016-07-12 45 views
0

在單擊listView中的按鈕後嘗試創建彈出窗口,然後通過單擊框外的按鈕關閉它。但是,當我嘗試點擊按鈕時,出現此錯誤。嘗試在自定義適配器的listView中創建彈出窗口時發生空指針異常

                    java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.IBinder android.view.View.getWindowToken()' on a null object reference 
                        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:897) 
                        at logistica.enviaflores.com.logistica.utilities.MyAdapter$1.onClick(MyAdapter.java:81) 
                        at android.view.View.performClick(View.java:4780) 
                        at android.view.View$PerformClick.run(View.java:19866) 
                        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:5254) 
                        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:903) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我不能準確理解我在做什麼錯,它拋出空指針異常。也許我錯誤地編寫了代碼,但即使在我回去查看之後,它似乎也是正確的。任何幫助將不勝感激。

public class MyAdapter extends BaseAdapter { 
    private Context mContext; 
    private List<Bean> mList; 
    private PopupWindow popUpWindow; 
    private LayoutInflater inflater; 

public MyAdapter(Context context,List<Bean> list){ 
    mContext=context; 
    mList=list; 


} 

@Override 
public int getCount() { 
    return mList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    //use convertView recycle 
    if(convertView==null){ 
     holder=new ViewHolder(); 
     convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false); 
     holder.textView= (TextView) convertView.findViewById(R.id.textView2); 
     holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2); 
     holder.information= (Button) convertView.findViewById(R.id.button5); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    //set text and url 
    holder.information.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      ViewGroup container = (ViewGroup) inflater.inflate(R.layout.information_popup, null); 
      popUpWindow = new PopupWindow(container, 400,400,true); 
//***I believe the problem happens right in the line under this sentence!*** 
      popUpWindow.showAtLocation(v.findViewById(R.id.orders), Gravity.CENTER, 0,0); 

      container.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View view, MotionEvent motionEvent) { 
        popUpWindow.dismiss(); 
        return true; 
       } 
      }); 
     } 
    }); 

    holder.textView.setText(mList.get(position).getText()); 
    Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView); 

    return convertView; 
} 

class ViewHolder{ 
    TextView textView; 
    ImageView imageView; 
    Button information; 
    Button close; 

    } 
} 
+1

請發佈完整的logcat,也許我們可以從中推斷出一些有用的東西。還要注意其中的行號,並嘗試突出顯示錯誤發生處的代碼行。 – Vucko

+0

@Vucko我現在已經發布了logcat。該錯誤發生在適配器內的onclick監聽器中。我認爲這會有點顯而易見,但是要感謝那些不明白的人。 :) – Kekis2014

+0

請確保在** v ** –

回答

2

而是在信息按鈕查看搜索R.id.orders的,搜索在convertView

舊代碼:

popUpWindow.showAtLocation(v.findViewById(R.id.orders), Gravity.CENTER, 0,0); 

新代碼:

popUpWindow.showAtLocation(convertView.findViewById(R.id.orders), Gravity.CENTER, 0,0); 
相關問題