2 sinat 33645256 sinat_33645256 于 2016.03.24 10:56 提问

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)

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

3个回答

u010090644
u010090644   2016.03.24 11:40

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

sinat_33645256
sinat_33645256 这行:vh.tv_content.setText(n.content);
一年多之前 回复
lxk_1993
lxk_1993   Rxr 2016.03.24 11:58

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();

sinat_33645256
sinat_33645256 回复Smile_KO: 真的没事了...
一年多之前 回复
lxk_1993
lxk_1993 回复sweetstar86: 不可能吧 你运行应该还有报错
一年多之前 回复
sinat_33645256
sinat_33645256 重启as竟然就没事了...
一年多之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.24 14:21

getLayoutInflater()改成LayoutInflater.from(context)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!