适配器getView 方法报了空指针

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhangtian6691844/article/details/51673512

适配器getView 方法报了空指针

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

ViewHolder holder=null;

if (convertView==null) {

convertView=inflater.inflate(R.layout.item_gridview, parent, false);

holder=new ViewHolder();

holder.imgView=(ImageView) findViewById(R.id.id_imgview);

holder.imgBtn=(ImageButton) findViewById(R.id.id_imgbtn);

convertView.setTag(holder);

}else{

holder=(ViewHolder) convertView.getTag();

}

holder.imgView.setImageResource(R.drawable.ic_dialog_alert);

holder.imgBtn.setImageResource(R.drawable.btn_check_on_disabled_holo_light);

ImageLoader.getImageLoader(3,Type.LIFO).loadImage(dirPath+"/"+imgPathList.get(position), holder.imgView);

return convertView;

}

class ViewHolder{

ImageView imgView;

ImageButton imgBtn;

}



09-07 10:47:40.062: E/AndroidRuntime(23353): FATAL EXCEPTION: main

09-07 10:47:40.062: E/AndroidRuntime(23353): Process: com.example.imooc_imageloader, PID: 23353

09-07 10:47:40.062: E/AndroidRuntime(23353): java.lang.NullPointerException

09-07 10:47:40.062: E/AndroidRuntime(23353): at com.example.imooc_imageloader.MainActivity$ImageAdapter.getView(MainActivity.java:100)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.AbsListView.obtainView(AbsListView.java:2263)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.GridView.onMeasure(GridView.java:1044)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.View.measure(View.java:16497)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.View.measure(View.java:16497)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.View.measure(View.java:16497)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

09-07 10:47:40.062: E/AndroidRuntime(23353): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.View.measure(View.java:16497)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)

09-07 10:47:40.062: E/AndroidRuntime(23353): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.View.measure(View.java:16497)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.Choreographer.doCallbacks(Choreographer.java:574)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.Choreographer.doFrame(Choreographer.java:544)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.os.Handler.handleCallback(Handler.java:733)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.os.Handler.dispatchMessage(Handler.java:95)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.os.Looper.loop(Looper.java:136)

09-07 10:47:40.062: E/AndroidRuntime(23353): at android.app.ActivityThread.main(ActivityThread.java:5017)

09-07 10:47:40.062: E/AndroidRuntime(23353): at java.lang.reflect.Method.invokeNative(Native Method)

09-07 10:47:40.062: E/AndroidRuntime(23353): at java.lang.reflect.Method.invoke(Method.java:515)

09-07 10:47:40.062: E/AndroidRuntime(23353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

09-07 10:47:40.062: E/AndroidRuntime(23353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

09-07 10:47:40.062: E/AndroidRuntime(23353): at dalvik.system.NativeStart.main

      




问题找到了,应该是

holder.imgView=(ImageView) convertView.findViewById(R.id.id_imgview);

holder.imgBtn=(ImageButton) convertView.findViewById(R.id.id_imgbtn);

或者

item选项不要写错了


展开阅读全文

SSH编程报了空指针

12-16

严重: Servlet.service() for servlet action threw exceptionrnjava.lang.NullPointerExceptionrn at com.dianxun.action.SendTieziAction.execute(SendTieziAction.java:54)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)rn at java.lang.Thread.run(Thread.java:619)rnrnrnAction里的代码:public ActionForward execute(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response) rn // TODO Auto-generated method stubrn SendTieziForm sendForm = (SendTieziForm) form;rn rn int id =Integer.parseInt(sendForm.getXid());rn String contxtString = sendForm.getTxtCon();rn String username = sendForm.getUname();rn System.out.println(id+","+contxtString+","+username);rn ArrayList list= personService.getTopicDate("from Topic where id = "+id);//这里报空指针了rn Topic topic2 = new Topic();rn topic2.setParented(id);rn topic2.setFroumid(list.get(0).getFroumid());rn topic2.setUsername(username);rn topic2.setTzcontent(contxtString);rn Timestamp t = new Timestamp(new Date().getTime());rn topic2.setFtimes(t);rn personService.save(topic2);rn return null;rn rnrnrngetTopicDate方法:rnpublic ArrayList getTopicDate(String sqlString)rn rn ArrayList list = new ArrayList();rn Query query = sessionFactory.getCurrentSession().createQuery(sqlString);rn List list2 = query.list();rn for( int i=0;i 论坛

关于适配器getView方法和ViewHolder使用遇到的一个问题

09-22

就是在getView方法中加了一个判读,如果新闻是已读的话,就把它设置成灰色,但是把读过的新闻设置成rn灰色的之后,在滚动view的时候,其它的新闻的颜色也会改变rnViewHolder的代码如下:rn[code=Java]rn//保存每条新闻的类rnpublic final class ViewHolderrn TextView newstitle;//新闻标题rn TextView updatetime;//新闻更新的时间rnrn[/code]rngetView方法代码:rn[code=Java]rnpublic View getView(int position, View convertView, ViewGroup parent) rn ViewHolder viewHolder=null;rn if(convertView==null)//第一次显示的时候rn convertView=mInflater.inflate(R.layout.newsitem, null);rn viewHolder=new ViewHolder();rn viewHolder.newstitle=(TextView)convertView.findViewById(R.id.newsTitle);rn viewHolder.updatetime=(TextView)convertView.findViewById(R.id.updateTime);rn convertView.setTag(viewHolder);rn else//之前已经显示过,再次显示rn viewHolder=(ViewHolder)convertView.getTag();rn rn NewInfo newsInfo = newsList.get(position);rn TextView title = viewHolder.newstitle;rn TextView updatetime = viewHolder.updatetime;rn rn title.setText(newsInfo.getTitle());rn updatetime.setText(newsInfo.getPubDate());rn title.setTextColor(R.color.blue);rn updatetime.setTextColor(R.color.lightslategrey);rn if(newsInfo.getRead_flag() == 1)rn Log.v("hachi","已读");rn title.setTextColor(R.color.grey);rn updatetime.setTextColor(R.color.grey);rn rn return convertView;rnrn[/code] 论坛

适配器总是出现空指针问题

08-12

问题描述: rn java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.administrator.myapplication14/com.example.administrator.myapplication14.Activity.FAdoptActivity: java.lang.NullPointerExceptionrn at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)rn at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)rn at android.app.ActivityThread.access$600(ActivityThread.java:162)rn at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)rn at android.os.Handler.dispatchMessage(Handler.java:107)rn at android.os.Looper.loop(Looper.java:194)rn at android.app.ActivityThread.main(ActivityThread.java:5407)rn at java.lang.reflect.Method.invokeNative(Native Method)rn at java.lang.reflect.Method.invoke(Method.java:525)rn at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)rn at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)rn at dalvik.system.NativeStart.main(Native Method)rn Caused by: java.lang.NullPointerExceptionrn at android.view.LayoutInflater.from(LayoutInflater.java:210)rn at com.example.administrator.myapplication14.Adapter.FAdoptAdapter.(FAdoptAdapter.java:27)rn at com.example.administrator.myapplication14.Activity.FAdoptActivity.initAdapter(FAdoptActivity.java:83)rn at com.example.administrator.myapplication14.Activity.FAdoptActivity.onCreate(FAdoptActivity.java:44)rn at android.app.Activity.performCreate(Activity.java:5234)rn at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)rnrnActivity:rnrnpackage com.example.administrator.myapplication14.Activity;rnrnimport android.content.Context;rnimport android.support.v7.app.AppCompatActivity;rnimport android.os.Bundle;rnimport android.support.v7.widget.Toolbar;rnimport android.util.Log;rnimport android.view.MenuItem;rnimport android.view.View;rnimport android.widget.AdapterView;rnimport android.widget.ListView;rnimport android.widget.Toast;rnrnimport com.example.administrator.myapplication14.Adapter.FAdoptAdapter;rnimport com.example.administrator.myapplication14.R;rnimport com.example.administrator.myapplication14.be.FAdoptImage;rnrnimport java.util.ArrayList;rnrnrnpublic class FAdoptActivity extends AppCompatActivity rn Toolbar toolbar;rn ListView listView;rn ArrayList arrayList = new ArrayList();rn FAdoptAdapter adapter;rn //View v;rn Context context;rnrn public FAdoptActivity()rnrn rnrn @Overridern protected void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.activity_fadopt2);rn Log.i("--oncreat--", "onCreate: ");rn initData();rn Log.i("---- initData();-----", "onCreate: ");rn initAdapter();rn Log.i("---- initAdapter();---", "onCreate: ");rn initToolbar();rn Log.i("----initToolbar();-----", "onCreate: ");rnrn rnrn private void initToolbar() rn Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar1);rn //设置导航栏图标rn toolbar.setNavigationIcon(R.mipmap.ic_launcher);rn //设置app logorn //toolbar.setLogo(R.mipmap.ic_launcher);rn //设置主标题rn toolbar.setTitle("领养");rn //设置右上角的填充菜单rn toolbar.inflateMenu(R.menu.fadopt_item);rn toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() rn @Overridern public boolean onMenuItemClick(MenuItem item) rn int menuItemId = item.getItemId();rn if (menuItemId == R.id.action_search) rn Toast.makeText(context, R.string.menu_search, Toast.LENGTH_LONG).show();rnrn else if (menuItemId == R.id.action_notification) rn Toast.makeText(context, R.string.menu_notifications, Toast.LENGTH_LONG).show();rnrn else if (menuItemId == R.id.action_item1) rn Toast.makeText(context, R.string.item_01, Toast.LENGTH_LONG).show();rnrn else if (menuItemId == R.id.action_item2) rn Toast.makeText(context, R.string.item_02, Toast.LENGTH_LONG).show();rn rn return true;rn rn );rn rnrn private void initAdapter() rn adapter=new FAdoptAdapter(context,arrayList);rn listView.setAdapter(adapter);rn listView.setOnItemClickListener(new AdapterView.OnItemClickListener() rn @Overridern public void onItemClick(AdapterView adapterView, View view, int i, long l) rnrn rn );rnrn rnrn private void initData() rn toolbar= (Toolbar) findViewById(R.id.toolbar1);rn listView= (ListView) findViewById(R.id.listView1);rn for (int i=1;i<10;i++)rn arrayList.add(new FAdoptImage(R.mipmap.ic_launcher,"name"+i,"meseage"+i));rn rn rn rnrnAdapter:rnrnrnpublic class FAdoptAdapter extends BaseAdapter rn Context context;rn ArrayList pet;rn LayoutInflater mLayoutInflater;rnrn public FAdoptAdapter(Context context, ArrayList pet) rn this.context = context;rn this.pet = pet;rn mLayoutInflater=LayoutInflater.from(context);rn rnrn @Overridern public int getCount() rn return pet.size();rn rnrn @Overridern public Object getItem(int i) rn return pet.get(i);rn rnrn @Overridern public long getItemId(int i) rn return i;rn rnrn class ViewHolderrn ImageView imageView;rn TextView textView1;rn TextView textView2;rn rn ViewHolder mViewHolder;rnrn @Overridern public View getView(int i, View view, ViewGroup viewGroup) rn // View v= LayoutInflater.from(context).inflate(R.layout.fadopt_list_item,null);rn if(view==null)rn view=mLayoutInflater.inflate(R.layout.fadopt_list_item,null);rn mViewHolder = new ViewHolder();rn mViewHolder.imageView= (ImageView) view.findViewById(R.id.imageView_item);rn mViewHolder.textView1= (TextView) view.findViewById(R.id.textView_name);rn mViewHolder.textView2= (TextView) view.findViewById(R.id.textView_juti);rn view.setTag(mViewHolder);rn else rn mViewHolder= (ViewHolder) view.getTag();rn rn mViewHolder.imageView.setImageResource((int) pet.get(i).getTouXiang());rn mViewHolder.textView1.setText(pet.get(i).getName());rn mViewHolder.textView2.setText(pet.get(i).getMeseage());rnrn return view;rn rnrnrn 论坛

没有更多推荐了,返回首页