2016-11-05 152 views
-2

我想看看这个问题每一个答案,但没有和我一起工作显示java.lang.NullPointerException:尝试在空对象引用调用虚方法”

我知道计数器视图返回NULL,但我不不知道为什么

试过类似的东西在的代码,但没有工作

 public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 



    LayoutInflater inflater=activity.getLayoutInflater(); 

    if(convertView == null){ 

     convertView=inflater.inflate(R.layout.column_row, null); 

     txtFirst=(TextView) convertView.findViewById(R.id.name); 
     txtSecond=(TextView) convertView.findViewById(R.id.gender); 
     txtThird=(TextView) convertView.findViewById(R.id.age); 
     //txtFourth=(TextView) convertView.findViewById(R.id.status); 

     if (txtFirst == null) { 
      txtFirst.setText("kkkk"); 
     } 
     if (txtSecond == null) { 
      txtSecond.setText("kllll"); 
     } 
     if (txtThird == null) { 
      txtThird.setText("llllll"); 
     } 

    } 
    return convertView; 
    } 

我logcat的

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 

我的代码

package com.example.bavly.dollorvalueindifferentboanksegypt; 



import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.HashMap; 

import static  
com.example.bavly.dollorvalueindifferentboanksegypt.constants.FIRST_COLUMN; 
import static 
com.example.bavly.dollorvalueindifferentboanksegypt.constants.SECOND_COLUMN; 
import static 
com.example.bavly.dollorvalueindifferentboanksegypt.constants.THIRD_COLUMN; 

public class ListViewAdapters extends BaseAdapter { 
public ArrayList<HashMap<String, String>> list; 
Activity activity; 
TextView txtFirst; 
TextView txtSecond; 
TextView txtThird; 
//TextView txtFourth; 
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){ 
    super(); 
    this.activity=activity; 
    this.list=list; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return list.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return list.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 



@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 



    LayoutInflater inflater=activity.getLayoutInflater(); 

    if(convertView == null){ 

     convertView=inflater.inflate(R.layout.column_row, null); 

     txtFirst=(TextView) convertView.findViewById(R.id.name); 
     txtSecond=(TextView) convertView.findViewById(R.id.gender); 
     txtThird=(TextView) convertView.findViewById(R.id.age); 
     //txtFourth=(TextView) convertView.findViewById(R.id.status); 


    } 

    HashMap<String, String> map=list.get(position); 
    txtFirst.setText(map.get(FIRST_COLUMN)); 
    txtSecond.setText(map.get(SECOND_COLUMN)); 
    txtThird.setText(map.get(THIRD_COLUMN)); 
    //txtFourth.setText(map.get(FOURTH_COLUMN)); 

    return convertView; 
} 

} 
+0

真的吗? 'if(txtFirst == null){txtFirst.setText(“kkkk”); }'。 –

+0

如果我删除它,我仍然有同样的问题 – Bav

+0

你的问题没有一个ListView,但它是空的,你应该读取异常并调试它是什么。 NullPointerException的原因总是相同的 –

回答

1

ListView控件为空。确认findViewById不返回空值。

+0

谢谢我搜索了你说什么 我发现有解决方案 http://stackoverflow.com/questions/3264610/findviewbyid-returns-null 有一个错误当它返回 主要活动 setContentView(R.layout.listvalue); – Bav

相关问题