listview-ListView中遇到的空指针异常

问题描述

ListView中遇到的空指针异常

以下代码段,异常在最下面:

 package com.sweetstar86.myapplicationlistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Vector;

public class MainActivity6 extends AppCompatActivity {
    private ListView listView;
    private Vector<News> news = new Vector<>();
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        listView = (ListView) findViewById(R.id.listView5);
        initData();
        myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
    }

    private int index = 1;

    /**
     * Init Data
     */

    private void initData() {
        for (int i = 0; i < 10; i++) {

            News n = new News();
            n.title = "title--" + index;
            n.content = "content--" + index;
            index++;
            news.add(n);
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return news.size();
        }

        @Override
        public Object getItem(int position) {
            return news.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh;
            if(convertView==null){
                convertView = getLayoutInflater().inflate(R.layout.list_item_6,null);
                vh = new ViewHolder();
                vh.tv_title = (TextView) convertView.findViewById(R.id.textView_title);
                vh.tv_content = (TextView) convertView.findViewById(R.id.textView_content);
                convertView.setTag(vh);
            }else{

                vh = (ViewHolder) convertView.getTag();
            }

            News n = news.get(position);
           vh.tv_title.setText(n.title);
           vh.tv_content.setText(n.content);
            return convertView;
        }

        class ViewHolder {
            TextView tv_title;
            TextView tv_content;
        }
    }
}

异常:
java.lang.NullPointerException
at com.sweetstar86.myapplicationlistview.MainActivity6$MyAdapter.getView(MainActivity6.java:77)
at android.widget.AbsListView.obtainView(AbsListView.java:2622)
at android.widget.ListView.makeAndAddView(ListView.java:1838)
at android.widget.ListView.fillDown(ListView.java:681)
at android.widget.ListView.fillFromTop(ListView.java:742)
at android.widget.ListView.layoutChildren(ListView.java:1659)
at android.widget.AbsListView.onLayout(AbsListView.java:2440)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1669)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1527)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14615)
at android.view.ViewGroup.layout(ViewGroup.java:4491)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2191)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2005)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1218)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4999)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5308)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

本人安卓新人,跟着教程学的,实在找不出来原因...

解决方案

77是哪行?
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.*, null); 试试

解决方案二:

convertView = LayoutInflater.from(MainActivity6.this).inflate(R.layout.item_cloud_help_list, null); 试试这个

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if(convertView==null){
convertView = getLayoutInflater().inflate(R.layout.list_item_6,null); //替换这句
vh = new ViewHolder();

解决方案三:

getLayoutInflater()改成LayoutInflater.from(context)

时间: 2024-12-25 22:18:11

listview-ListView中遇到的空指针异常的相关文章

android-在 ListView Footer 中的 Enter Click 监听器

问题描述 在 ListView Footer 中的 Enter Click 监听器 我想在 listView 中设置一个 edit text,作为一个footer,那样的话我就能添加一个评论.我可以把edittext 添加到listview中,但是没有看到我设置的监听器.为什么啊? editText = new EditText(getActivity()); editText.setHint("add comment"); editText.setBackgroundColor(Co

关于如何在listview监听器中如何获取所点击那部分的值

问题描述 关于如何在listview监听器中如何获取所点击那部分的值 我在listview绑定了三个textview, 我想在我点击listview的时候把我点击的项中,取得它里面的其中一个textview的值, 然后传递到另一个activity 我的监听器代码如下 OnItemClickListener listener =new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, Vi

监听-listview item中是有EditText改变时获取Postion?

问题描述 listview item中是有EditText改变时获取Postion? listview item中是有EditText, 怎么在监听每个EditText的时候获取这个EditText的Postion? 解决方案 getview时,可以给这个edittext的tag赋值postion,然后取出tag得到postion 解决方案二: 不是本地爸爸的话大把大把差不多你都能 解决方案三: 用ListView 不要重用ConvertView

android listview item中超链接的实现

问题描述 android listview item中超链接的实现 我想在listview的textview中实现超链接,但是没有反应,是怎么回事?还有个问题,就是适配器里面的内容怎么执行了5遍? public class FishWebActivity extends Activity { private Context context; private TextView text_title; private ListView web_list; @Override protected vo

java-Android的listview问题中的adapter

问题描述 Android的listview问题中的adapter @Override public View getView(int position, View convertView, ViewGroup parent) { convertView =LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null); if(convertView!=null) { textview=(TextView)

android 中的listview item中如何放置一个高度超过item的imageview

问题描述 android 中的listview item中如何放置一个高度超过item的imageview android 中的listview item中如何放置一个高度超过item的imageview. 现在项目有需求,需要在一个listview的item中放置一个imageview,而imageview的高度要 超过这个item,是他看起来只显示imageview的一部分.大伙有思路吗? 解决方案 [android]ListView的item高度调整android给listview的ite

Android中 ScrollView(ListView)中嵌套ListView时显示不全的简便解决方案

利用下面这个Utility 类的静态方法setListViewHeightBasedOnChildren()即可实现: 在listview.setAdapter()之后调用Utility.setListViewHeightBasedOnChilren(listview)就Okay 了. public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { //获取ListVi

PinnedSectionListView:分组的listView滑动中固定组标题的实现

在很多应用中,看到这样的listview:listview滑动过程中分组标题固定在上方,当第二个组滑上来时,第一个组才跟着上滑,下一个组固定,直到该组也滑出上边缘.世上无难事只怕有心人,在github上就有人做出来了,而且效果很好(后来发现安卓自带应用中联系人应用就是这样的,估计github的作者也是仿照着联系人做出来的吧). 先看截图:      PinnedSectionListView继承自listview,众所周知listview的每个子view都是按顺序跟着滚动的,要实现联系人list

关于hibernate 中query.list()空指针异常,急!!!!在线等,望达人解决下

问题描述 publicclassCollegeDaoImplTestextendsTestCase{publicvoidtestCollegeDaoImpl(){Sessionsession=HibernateUtils.getSession();session.beginTransaction();Queryquery=session.createQuery("selectcfromCollegec");for(inti=0;i<query.list().size();i++)