2016-12-29 54 views
-2

在這裏,我將XML代碼通過使用在getView方法如下聲明:錯誤:嘗試在空對象引用上調用虛擬方法'java.lang.Object android.content.Context.getSystemService(java.lang.String)'參考

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) 

一些能幫我解決上面說的問題?

回答

0

在您的構造函數中,您將自己設置爲context。我想你想要的是將其設置爲mainActivity

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