2015-04-12 94 views
1

繼其他帖子關於同一主題我實施了getView陣列適配器的方法。但由於突出顯示的行,該應用程序不幸停止,因爲如果我將其註釋掉,應用程序不會崩潰。好心幫什麼錯: -無法更改ListView項目的文本顏色

aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, al) { 
       @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      ***TextView tv = (TextView)findViewById(android.R.id.text1);*** 
      tv.setTextColor(Color.WHITE); 
      tv.setVisibility(View.VISIBLE); 

      return view; 
     } 

的logcat -

4月4日至一十三日:48:38.246 2870至2870年/ com.example.sairav.app3 E/AndroidRuntime:致命異常:主要 過程:COM .example.sairav.app3,PID:2870 java.lang.NullPointerException:嘗試在com.example的空對象引用 上調用虛擬方法'android.view.View android.view.View.findViewById(int)'。 sairav.app3.MainActivity $ 1.getView(MainActivity.java:41) at android.widget.AbsListView.obtainView(AbsListView.java:2344) at android.widget.ListView.makeAn在android.widget.ListView.fillDown(ListView.java:698) at android.widget.ListView.fillFromTop(ListView.java:759) at android.widget.ListView.layoutChildren( ListView.java:1673) 在android.widget.AbsListView.onLayout(AbsListView.java:2148) 在android.view.View.layout(View.java:15596) 在android.view.ViewGroup.layout(ViewGroup中。 (ViewGroup.java:java:4966) at android.view.View.Group.layout 4966) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573) at android.widget .FrameLayout.onLayout(FrameLayout.java:508) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at com.android.internal .widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494) at android.view.View.layout(View.java:15596) at android.view.ViewGroup.layout(ViewGroup.java:4966) at android.widget .FrameLayout.layoutChildren在android.view.ViewGroup(FrameLayout.java:573) 在android.widget.FrameLayout.onLayout(FrameLayout.java:508) 在android.view.View.layout(View.java:15596) .layout(ViewGroup.java:4966) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054) at android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5779) (Choreographer.java:567) at android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) (Handler.java:95) at android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753) 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 java.lang.reflect.Method.invoke(Method.java:372) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit。Java的:694)

需要做什麼?嘗試在空對象引用上調用虛擬方法'android.view.View android.view.View.findViewById(int)'

回答

0

當你的convertView爲空,你需要加載一個新的。

技巧:

aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, al) { 
        @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       if (convertView == null) { 
        convertView = LayoutInflater.from(context).inflate(
         R.layout.fragment_home_drawer_listview_item, null); 
       } 
       TextView tv = (TextView)convertView.findViewById(android.R.id.text1); 
       tv.setTextColor(getResources.getColor(Color.WHITE)); 
       tv.setVisibility(View.VISIBLE); 
       return convertView; 
      } 
0

您應該在傳遞給getView的視圖上調用findViewById。

變化

TextView tv = (TextView)findViewById(android.R.id.text1); 

TextView tv = (TextView) convertView.findViewById(android.R.id.text1); 
+0

啊工作,試了一下也還是停止不幸! :( – user3820753

+0

你可以張貼日誌 – Noob

+0

完成,空指針異常嗯 – user3820753

0

試試這個,

aa = new ArrayAdapter(this,android.R.layout.simple_list_item_1, al) { 
        @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       View view = super.getView(position, convertView, parent); 
       ***TextView tv = (TextView)findViewById(android.R.id.text1);*** 
       tv.setTextColor(getResources.getColor(Color.WHITE)); 
       tv.setVisibility(View.VISIBLE); 

       return view; 
      } 
0

根據您的logcat,查看變量爲NULL,因爲super.getView沒有 什麼回報。

你可以改變你ArrayAdapter創造這樣的:

new ArrayAdapter (context, android.R.layout.simple_list_item_2, android.R.id.text1, list) 
{ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    ... your code goes here. 
    ... 
    } 
} 
+0

仍然會崩潰:\ – user3820753

0

這將在列表視圖中使用適配器

txtview.setTextColor(activity.getResources().getColor(R.color.red));