2017-08-10 78 views
1

我想爲getView()中的Textview設置OnClickListener - 擴展ArrayAdapter的類的方法。但是,我只能cllick前三個項目,他們後我得到一個NullPointerException。爲什麼?在ArrayAdapter中設置OnClickListener <>

驗證碼:

content = (TextView) view.findViewById(R.id.list_layout); 
    content.setText(currentLayout.getContent()); 

    public View getView(final int position, @Nullable final View convertView, @NonNull ViewGroup parent) { 
    currentLayout = getItem(position); 

    View view = convertView; 

    if (view == null) { 
     view = LayoutInflater.from(getContext()).inflate(R.layout.rowlayout, parent, false); 
    } 

    content = (TextView) view.findViewById(R.id.list_layout); 
    content.setText(currentLayout.getContent()); 

    number = view.findViewById(R.id.number); 

    layout = view.findViewById(R.id.layout); 

    content.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      convertView.setBackgroundColor(Color.YELLOW); 
     } 
    }); 

    number.setText("1"); 

    return view; 
} 

的崩潰reprt:

--------- beginning of crash 
08-10 15:18:45.591 3845-3845/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.example.philipp.einkaufsliste110, PID: 3845 
               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference 
                at com.example.philipp.einkaufsliste110.Adapter.CustomAdapter$1.onClick(CustomAdapter.java:63) 
                at android.view.View.performClick(View.java:5610) 
                at android.view.View$PerformClick.run(View.java:22265) 
                at android.os.Handler.handleCallback(Handler.java:751) 
                at android.os.Handler.dispatchMessage(Handler.java:95) 
                at android.os.Looper.loop(Looper.java:154) 
                at android.app.ActivityThread.main(ActivityThread.java:6077) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
08-10 15:18:45.592 1651-2151/system_process W/ActivityManager: Force finishing activity com.example.philipp.einkaufsliste110/.MainActivity 
08-10 15:18:45.606 1305-3578/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property 

               [ 08-10 15:18:45.627 1651: 4136 D/   ] 
               HostConnection::get() New Host Connection established 0x8e578440, tid 4136 


               [ 08-10 15:18:45.628 1651: 4136 W/   ] 
               Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 
+0

顯示堆棧跟蹤 –

+1

可以粘貼異常 – Swati

+0

異常粘貼到更好的幫助。 –

回答

0

你是不是檢查空的convertview。

你只能用convertview初始化視圖。但只有初始化視圖,如果它爲null,但如果它爲null,則不初始化convertview。

爲什麼不能使用視圖來設置背景而不是convertview?

您可以使用查看持有者作爲listview的更好的做法。參考 - https://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

此外,您還可以做設置監聽如下 -

if (convertView == null) { 
     convertView = inflater.inflate(/**layout file**/, parent, false); 
     viewHolder = new ViewHolder(); 
     viewHolder.content = (TextView) view.findViewById(R.id.list_layout); 

     viewHolder.content.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       convertView.setBackgroundColor(Color.YELLOW); 
     }); 
     vi.setTag(viewHolder); 
    } else { 
     // else code goes here 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
+0

謝謝。但是什麼是觀察者? – Sarius

+0

您可能想要查看實現ListView的最佳實踐。請訪問回答中提到的鏈接查看ViewHolder。 – Swati

+0

好的,謝謝 – Sarius

相關問題