2015-07-02 50 views
0

我正在嘗試爲我的導航抽屜製作一個自定義適配器,並且在佈局充填器上不斷得到一個空指針異常。我試過在getView方法內部和外部聲明LayoutInflater變量,如其他帖子中的建議,但無濟於事。Inflater在自定義抽屜適配器上的空指針異常

下面是我的自定義適配器類

Context mContext; 
private ArrayList<String> mValues = new ArrayList<String>(); 
LayoutInflater inflater; 

public DrawerListAdapter(Context context, ArrayList<String> mValues) { 
    super(context, R.layout.drawer_layout, mValues); 

    this.mContext = mContext; 
    this.mValues = mValues; 
    inflater = LayoutInflater.from(mContext); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View rowView = convertView; 

    if(rowView == null){ 
     rowView = inflater.inflate(R.layout.drawer_layout, parent, false); 
    } else{ 
     Log.d("DEBUG", "rowView is not null"); 
    } 

    TextView textView = (TextView) rowView.findViewById(R.id.drawer_text); 

    switch (position) { 
     case 0: 
      textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialRed)); 
      textView.setText(mValues.get(position)); 
      break; 
     case 1: 
      textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLightBlue)); 
      textView.setText(mValues.get(position)); 
      break; 
     case 2: 
      textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLightGreen)); 
      textView.setText(mValues.get(position)); 
      break; 
     case 3: 
      textView.setBackgroundColor(mContext.getResources().getColor(R.color.materialLime)); 
      textView.setText(mValues.get(position)); 
      break; 

    } 

    return rowView; 
} 

這裏就是我的onCreate()方法

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
    setContentView(R.layout.activity_main); 

    mDrawerTitles.add("Politics"); 
    mDrawerTitles.add("Technology"); 
    mDrawerTitles.add("Sports"); 
    mDrawerTitles.add("Books"); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(android.R.id.list); 

    DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mDrawerTitles); 
    mDrawerList.setAdapter(drawerAdapter); 

    //mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
    new HttpJSONLoader().execute(); 

} 

和我的堆棧跟蹤中調用它在我的MainActivity

Caused by: 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 net.rmoreno.lemonlime.DrawerListAdapter.<init>(DrawerListAdapter.java:26) 
     at net.rmoreno.lemonlime.MainActivity.onCreate(MainActivity.java:49) 
     at android.app.Activity.performCreate(Activity.java:5933) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 

回答

1

此行this.mContext = mContext;應在DrawerListAdapter構造函數中爲mContext = context;

+0

這本身並沒有做過 – Rafa

+0

嘗試https://developer.android.com/ tools/debugging/debug-studio.html來查看哪些變量爲空。 – Emma

0

變化是

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    Context = parent.getContext() 

    /** then check convertView & inflate view & u can use placeHolder with setTag()*/ 
    rootView = LayoutInflater.from(context).inflate(R.layout....,parent,false); 
    .... 
    return rootView; 
} 

這裏你長了例如如何管理適配器和AbsView

Example of ListAdapter AbsView & PlaceHolder pattern