title: Android imagebutton in custom listview is null
body: <p>i am developing an android app, which displays a listview. When i click some of the items of the listview it should open a dialog, which includes another (custom) listview. the listview in the dialog has two textviews and one (image)button (i tried both). but i am getting always a nullpointerException and i have no idea, what i am doing wrong. </p>
  
  <p>here is my code</p>
  
  <pre><code>    public class Schuldet_mir extends Fragment {
  
      private ListView mainList;
      private ListView subList;
      .....
  
      MainActivity mainActivity = (MainActivity) getActivity();
      .....
  
  
      public Schuldet_mir(int position) {                                                                         
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         //onCreate
  
          View v = inflater.inflate(R.layout.fragment_schuldet_mir, container, false);
  
          mainList        = (ListView)v.findViewById(R.id.listView1);
          imageButton     = (ImageButton)v.findViewById(R.id.imageBtn1);
  
  
          imageButton.setOnClickListener(new OnClickListener() {                                      
              @Override
              public void onClick(View v) {
  
                  openDialog();
              }
          });
  
          mainList.setOnItemClickListener(new OnItemClickListener() {                                             //Hauptliste klick
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long idInDB) {
  
                  mainListViewClick(parent, v, position, idInDB);
              }           
          });
  
          return v;
      }
  
      @Override                                                                                                   //onResume
      public void onResume() {
          super.onResume();
  
          populateListViewFromDB();   
      }
  
  
      private void openDialog() {                                                                                 
  
          final Dialog dialog1 = new Dialog(getActivity());                   
          dialog1.setContentView(R.layout.dialog1);
          dialog1.setTitle("Schuldner hinzufügen");
          dialog1.setCancelable(false);
  
          eintrag_name   = (EditText)dialog1.findViewById(R.id.editText_name);
          eintrag_betrag = (EditText)dialog1.findViewById(R.id.editText_betrag);
          eintrag_grund  = (EditText)dialog1.findViewById(R.id.editText1);
  
          ImageButton hinzufügen = (ImageButton)dialog1.findViewById(R.id.imageButton_add);
          ImageButton abbrechen = (ImageButton)dialog1.findViewById(R.id.imageButton_cancel);
  
          hinzufügen.setOnClickListener(new OnClickListener() {                                                   //Buttonklick Hinzufügen 
  
              @Override
              public void onClick(View v) {
                                                                  //do s.th.
  
                  }
              }
          });
  
  
          abbrechen.setOnClickListener(new OnClickListener() {                                                    
  
              @Override
              public void onClick(View v) {
                  dialog1.dismiss();                      
              }
          });
  
          dialog1.show();                 
      }
  
  
      private void mainListViewClick(AdapterView&lt;?&gt; parent, View v, int position, long idInDB) {                  
  
          final Dialog dialog3 = new Dialog(getActivity());                                                       
          dialog3.setContentView(R.layout.dialog3);
          dialog3.setTitle("Schuldner");
          dialog3.setCancelable(false);
  
          subList                 = (ListView)dialog3.findViewById(R.id.dialog3_listView);
          Button deleteBtn        = (Button)dialog3.findViewById(R.id.dialog3_deleteBtn);
          Button allesErhalten    = (Button)dialog3.findViewById(R.id.dialog3_btn1);
          Button zurück           = (Button)dialog3.findViewById(R.id.dialog3_btn2);
  
          populateSubListViewFromDB(idInDB);
  
          subList.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long idInDB) {
  
                  subListViewClick(parent, v, position, idInDB);              
              }
          });
  
          deleteBtn.setOnClickListener(new OnClickListener() {  **// line:193**                                                       /
  
              @Override
              public void onClick(View v) {
                  Log.e("++++++++", "TEST OK");                       
              }
          });
  
          allesErhalten.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
              }
          });
  
          zurück.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  dialog3.dismiss();
              }
          });
  
          dialog3.show(); 
      }
  
  //  --------------------------------------------------------------------
      private void subListViewClick(AdapterView&lt;?&gt; parent, View v, int position, long idInDB) {                   // SubList Item Click
  
          cursor = mainActivity.myDBHandler.getRowinTable1(idInDB);       
  
          if(cursor.moveToFirst()) {                                                                              //informationen der zeile
              idDB = cursor.getLong(DataBaseHandler.COL_ROWID);
              dialog2_name = cursor.getString(DataBaseHandler.COL_NAME);
              dialog2_betrag = cursor.getFloat(DataBaseHandler.COL_BETRAG);
              dialog2_grund = cursor.getString(DataBaseHandler.COL_GRUND);
              dialog2_datum = cursor.getString(DataBaseHandler.COL_DATUM);            
          }
  
          final Dialog dialog2 = new Dialog(getActivity());                                                       //open dialog
          dialog2.setContentView(R.layout.dialog2);
          dialog2.setTitle("Schuldner");
          dialog2.setCancelable(false);
  
          TextView dial2_TxtDatum  = (TextView)dialog2.findViewById(R.id.dialog2_TxtDatum);
          TextView dial2_TxtName   = (TextView)dialog2.findViewById(R.id.dialog2_TxtName);
          TextView dial2_TxtGrund  = (TextView)dialog2.findViewById(R.id.dialog2_TxtGrund);
          TextView dial2_TxtBetrag = (TextView)dialog2.findViewById(R.id.dialog2_TxtBetrag);
  
          Button erhalten = (Button)dialog2.findViewById(R.id.dialog2_btnBezahlt);
          Button ok = (Button)dialog2.findViewById(R.id.dialog2_btnOk);
  
          NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.getDefault());
          String currencyOut = currencyFormatter.format(dialog2_betrag);
  
          dial2_TxtDatum.setText("Vom " + dialog2_datum + " Uhr");
          dial2_TxtName.setText(dialog2_name);
          dial2_TxtGrund.setText(dialog2_grund);
          dial2_TxtBetrag.setText(currencyOut);
  
  
          erhalten.setOnClickListener(new OnClickListener() {                                                     //Buttonklick für erhalten
              @Override
              public void onClick(View v) {
  
                  mainActivity.myDBHandler.deleteRowInTable1(idDB);
                  populateListViewFromDB();
                  dialog2.dismiss();
              }
          });
  
          ok.setOnClickListener(new OnClickListener() {                                                           //Buttonklick für ok
              @Override
              public void onClick(View v) {
  
                  dialog2.dismiss();
              }
          });
  
          dialog2.show();
      }
  
  //  --------------------------------------------------------------------
      @SuppressWarnings("deprecation")
      public void populateListViewFromDB() {              ...
      }   
  
  //  --------------------------------------------------------------------
      @SuppressWarnings("deprecation")
      public void populateSubListViewFromDB(long idInDB) {        
  
      getActivity().startManagingCursor(cursor = mainActivity.myDBHandler.getAllEntriesFromAPerson1(idInDB));
  
      String[] dbSpaltenNamen = new String[] {"d", "b"};
  
      int[] zuListViewIDs = new int[] {R.id.dialog3_datum, R.id.dialog3_lz_betrag};
  
      SimpleCursorAdapter myCursorAdapter 
              = new SimpleCursorAdapter
                          (getActivity(), R.layout.dialog3_listzeile, cursor, dbSpaltenNamen, zuListViewIDs);
  
      subList.setAdapter(myCursorAdapter);
      }
  
  //  --------------------------------------------------------------------
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          try {
              mainActivity = (MainActivity) activity;
          } 
          catch (ClassCastException e) {
              throw new ClassCastException(activity.toString() +  " Fehler: Keine MainActivity-Instanz!");
          }
      }
  }
  </code></pre>
  
  <p>the logcat message:</p>
  
  <pre><code>01-10 18:10:19.392: E/AndroidRuntime(14649): FATAL EXCEPTION: main
  01-10 18:10:19.392: E/AndroidRuntime(14649): java.lang.NullPointerException
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at fragments.Schuldet_mir.mainListViewClick(Schuldet_mir.java:193)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at fragments.Schuldet_mir.access$1(Schuldet_mir.java:171)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at fragments.Schuldet_mir$2.onItemClick(Schuldet_mir.java:90)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.widget.AbsListView.performItemClick(AbsListView.java:1111)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2900)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.widget.AbsListView$1.run(AbsListView.java:3728)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.os.Handler.handleCallback(Handler.java:615)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.os.Handler.dispatchMessage(Handler.java:92)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.os.Looper.loop(Looper.java:137)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at android.app.ActivityThread.main(ActivityThread.java:4872)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at java.lang.reflect.Method.invokeNative(Native Method)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at java.lang.reflect.Method.invoke(Method.java:511)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
  01-10 18:10:19.392: E/AndroidRuntime(14649):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>the line 193 is the deleteBtn.setOnClickListener method. what i am doing wrong!? thanks in advance for advices</p>
  
  <p>EDIT:
  dialog3.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;ListView
      android:id="@+id/dialog3_listView"
      android:layout_width="match_parent"
      android:layout_height="0dip"
      android:layout_weight="0.98" &gt;
  &lt;/ListView&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="46dp" &gt;
  
      &lt;Button
          android:id="@+id/dialog3_btn1"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          android:layout_weight="1"
          android:text="Alles erhalten" /&gt;
  
      &lt;Button
          android:id="@+id/dialog3_btn2"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_gravity="bottom"
          android:layout_weight="1"
          android:text="Zurück" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>dialog3_listzeile: that is the custom row for the listview</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;TextView
      android:id="@+id/dialog3_datum"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:text="Medium Text"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:textStyle="italic" /&gt;
  
  &lt;Button
      android:id="@+id/dialog3_deleteBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:focusable="false"
      android:text="Delete" /&gt;
  
  &lt;TextView
      android:id="@+id/dialog3_lz_betrag"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/dialog3_deleteBtn"
      android:layout_centerHorizontal="true"
      android:text="Large Text"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textStyle="bold" /&gt;
  </code></pre>
  
  <p></p>