问题描述
- 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)