2017-05-27 96 views
-3

我有一個帶有activity_main.xml佈局的MainActivity。我有一個自定義的ListView,這使得自己的佈局每個ListView項:嘗試訪問ListView佈局按鈕時出現NullPointerException

ItemAdapter.java

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Loop loop = getItem(position); 

    // Check if an existing view is being reused, otherwise inflate the view 
    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false); 
    } 

在這種list_item.xml佈局,我有一個ImageButton的:

<ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_more_vert_black_24dp" 
     android:contentDescription="@string/descr_overflow_button" 
     android:id="@+id/overflowMenu" 
     android:layout_alignParentRight="true"/> 

在MainActivity的onCreate。我想設置一個OnClickListener到的ImageButton:

final ImageButton overflowMenu = (ImageButton) findViewById(R.id.overflowMenu); 
    overflowMenu.setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View arg0) { 
      LayoutInflater layoutInflater 
        = (LayoutInflater)getBaseContext() 
        .getSystemService(LAYOUT_INFLATER_SERVICE); 
      View popupView = layoutInflater.inflate(R.layout.item_popup, null); 
      final PopupWindow popupWindow = new PopupWindow(
        popupView, 
        Toolbar.LayoutParams.WRAP_CONTENT, 
        Toolbar.LayoutParams.WRAP_CONTENT); 

      popupWindow.showAsDropDown(overflowMenu, 50, -30); 

     }} 
    ); 

但是,顯然對在MainActivity的onCreate該ListView的佈局尚未產生,因此該ImageButton的不能被發現,我猜?其結果是,我得到這個錯誤:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

所以我怎麼設置OnClickListener這個ImageButton的?

+0

SE在getView方法中使用onclicklistner –

回答

0

嘗試將該ImageButton的OnClickListener放置在Adapter的getView()方法內。

您可以使用查看氣筒

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    // INFLATE A VIEW 
    View v=View.inflate(context,layout,null); 
    // GET BUTTON HANDLER 
    ImageButton overflowMenu = (ImageButton)v.findViewById(R.id.overflowMenu); 
    // HANDLE CLICK EVENT 
    overflowMenu.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // DO YOUR WORK HERE 
     } 
    }); 
    // RETURN THAT VIEW 
    return v; 
} 

希望它解決

+0

getBaseContext怎麼樣? – Donato

0

替換該行

final ImageButton overflowMenu = (ImageButton) findViewById(R.id.overflowMenu); 

這一行

final ImageButton overflowMenu = (ImageButton) convertView.findViewById(R.id.overflowMenu); 
+0

getBaseContext怎麼樣? – Donato

+0

// intitialize this; 上下文activity_context; LayoutInflater inflater; //構造函數 public ItemAdapter(Context con) { activity_context = con; inflater =(LayoutInflater)activity_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } //在getView中誇大這個 convertView = inflater.inflate(R.layout.list_item,null); – MageNative

相關問題