错误:尝试调用虚方法' java.lang.Object android.content.Context.getSystemService(java.lang.String)'在null对象引用上

时间:2016-12-29 16:15:34

标签: android listview firebase

这里我使用getView方法中的以下语句将XML代码扩展为java对象:

convertview= LayoutInflater.from(context).inflate(R.layout.model,parent,false); 

我的适配器如下所示:

public class CustomAdapter extends BaseAdapter {
    Context context;
    ArrayList<Spacecraft> space;

    public CustomAdapter(MainActivity mainActivity, ArrayList<Spacecraft> space) {
        this.context = context;
        this.space = space;
    }

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

    @Override
    public Object getItem(int i) {
        return space.get(i);
    }

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

    @Override
    public View getView(int i, View convertview, ViewGroup parent) {

        if (convertview==null)
        {
            convertview= LayoutInflater.from(context).inflate(R.layout.model,parent,false);
        }
        TextView name = (TextView) convertview.findViewById(R.id.txtd_name);
        TextView desc = (TextView) convertview.findViewById(R.id.txtd_desc);

        final Spacecraft s = (Spacecraft) this.getItem(i);
        name.setText(s.getMovie_name());
        desc.setText(s.getMovie_desc());

        convertview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, s.getMovie_name(), Toast.LENGTH_SHORT).show();
            }
        });

        return convertview;
    }
}

我的XML文件如下所示: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txtd_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/txtd_desc"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
上述适配器的

结果:抛出致命异常

convertview= LayoutInflater.from(context).inflate(R.layout.model,parent,false);

并抛出错误

12-29 21:26:58.869 16101-16101/com.application.custom_lstview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.application.custom_lstview, PID: 16101
java.lang.NullPointerException: 
Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference


at android.view.LayoutInflater.from(LayoutInflater.java:219)
at com.application.custom_lstview.view.CustomAdapter.getView(CustomAdapter.java:50)
at android.widget.AbsListView.obtainView(AbsListView.java:2467)
at android.widget.ListView.makeAndAddView(ListView.java:1894)
at android.widget.ListView.fillDown(ListView.java:710)
at android.widget.ListView.fillFromTop(ListView.java:771)
at android.widget.ListView.layoutChildren(ListView.java:1695)
at android.widget.AbsListView.onLayout(AbsListView.java:2230)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1195)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1722)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639)
at android.widget.FrameLayout.onLayout(FrameLayout.java:574)
at android.view.View.layout(View.java:16001)
at android.view.ViewGroup.layout(ViewGroup.java:5181)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2484)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2181)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1293)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6599)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:572)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

有人可以帮我解决上述问题吗?

1 个答案:

答案 0 :(得分:0)

在构造函数中,您将context设置为自身。我想你想要的是将它设置为mainActivity

public CustomAdapter(MainActivity mainActivity, ArrayList<Spacecraft> space) {
    this.context = mainActivity; // Before: this.context = context;
    this.space = space;
}