我正在创建一个listview,它有两个部分,两个部分都有不同的xml,但我的getView方法不起作用,给出以下异常: -
10-03 14:34:29.421: E/AndroidRuntime(11029): FATAL EXCEPTION: main
10-03 14:34:29.421: E/AndroidRuntime(11029): java.lang.NullPointerException
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.AbsListView.obtainView(AbsListView.java:1594)
10-03 14:34:29.421: E/AndroidRuntime(11029): at an droid.widget.ListView.makeAndAddView(ListView.java:1782)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.ListView.fillDown(ListView.java:705)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.ListView.fillFromTop(ListView.java:762)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.ListView.layoutChildren(ListView.java:1619)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.AbsListView.onLayout(AbsListView.java:1422)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at andr oid.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.View.layout(View.java:7175)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.ViewRoot.performTraversals(ViewRoot.java:1146)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.os.Looper.loop(Looper.java:130)
10-03 14:34:29.421: E/AndroidRuntime(11029): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-03 14:34:29.421: E/AndroidRuntime(11029): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 14:34:29.421: E/AndroidRuntime(11029): at java.lang.reflect.Method.invoke(Method.java:507)
10-03 14:34:29.421: E/AndroidRuntime(11029): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-03 14:34:29.421: E/AndroidRuntime(11029): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-03 14:34:29.421: E/AndroidRuntime(11029): at dalvik.system.NativeStart.main(Native Method)
我正在使用此代码: -
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(issection)
{
issection=false;
convertView=null;
}
if (convertView == null) {
final MyJobListModle objitem = data.get(position);
if (objitem != null && objitem.getIssection()!=null && !objitem.getIssection().equals("")) {
if (objitem.getIssection().equalsIgnoreCase("true")) {
convertView = objinflater.inflate(R.layout.listviewsection,
null);
holder = new ViewHolder();
issection=true;
convertView.setOnClickListener(null);
convertView.setOnLongClickListener(null);
convertView.setLongClickable(false);
final TextView sectionView = (TextView) convertView
.findViewById(R.id.txtsection);
sectionView.setText(objitem.getHeader_title());
}
}
else
{
convertView = objinflater.inflate(R.layout.postjobrow, null);
holder = new ViewHolder();
holder.objjobname = (TextView) convertView.findViewById(R.id.textjob);
holder.objbidalerts = (TextView) convertView
.findViewById(R.id.textalert);
holder.objclick =(RelativeLayout)convertView.findViewById(R.id.textalertclick);
holder.bidalerttext=(TextView)convertView.findViewById(R.id.alerttext);
holder.objarrowimageView = (ImageView) convertView
.findViewById(R.id.imagearrow);
convertView.setTag(holder);
}
}else {
holder = (ViewHolder) convertView.getTag();
}
/*---- Handle click on listview item ----*/
if(holder!= null && holder.objarrowimageView!=null)
{
holder.objarrowimageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String jobcode = data.get(position).getId();
Bundle objbundle = new Bundle();
Intent objintent = new Intent(objactivity,EditPostJobSection.class);
objbundle.putString("jobid",jobcode);
objbundle.putString("from","myjobadapter");
objbundle.putString("createdAt",data.get(position).getCreated());
objintent.putExtras(objbundle);
objactivity.startActivity(objintent);
}
});
}
if(holder!= null && holder.objclick!=null)
{
holder.objclick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String bidcount = data.get(position).getBid_count();
if(!bidcount.equals("0"))
{
System.out.println("ok");
ArrayList<JobBidAlertModle> objjobbidalertlist = data.get(position).getObjbidalertmodlelist();
Bundle objbunlde = new Bundle();
Intent objintent = new Intent(objactivity,MyJobBidAlerts.class);
objbunlde.putSerializable("objbidalertlist", objjobbidalertlist);
if(data!=null && data.size()>position)
{
objbunlde.putString("title",data.get(position).getTitle());
}
objbunlde.putString("createdAt",data.get(position).getCreated());
objbunlde.putString("from","myjobadapter");
if(data!=null && data.size()!=0)
{
objbunlde.putString("lat",data.get(position).getLocationlat());
objbunlde.putString("longi",data.get(position).getLocationlongi());
}
objintent.putExtras(objbunlde);
objactivity.startActivity(objintent);
}
if(bidcount.equals("0"))
{
}
}
});
}
if(holder!= null && holder.objjobname!=null && holder.objbidalerts!=null )
{
holder.objjobname.setText(data.get(position).getTitle());
String bidcount = data.get(position).getBid_count();
if(bidcount.equals("0"))
{
holder.objbidalerts.setVisibility(View.INVISIBLE);
}
else
{
holder.objbidalerts.setVisibility(View.VISIBLE);
holder.objbidalerts.setText(bidcount);
}
}
return convertView;
}
当我执行空指针时,我无法解决确切的问题。 请有人建议我。谢谢很多
答案 0 :(得分:2)
您可能没有在TextView
文件中定义R.id.txtsection
个listviewsection.xml
个。{/ p>