ListView BaseAdapter抛出NullPointer,我不知道为什么

时间:2015-02-05 13:19:56

标签: android android-layout

好吧,我正在尝试使用fontawesome图标设置抽屉菜单。 这就是我所拥有的:

public class MotoristaAutonomoHome extends ActionBarActivity {

private DrawerLayout mDrawerLayout;
private ListView menuLateral;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_motorista_autonomo_home);

    menuLateral = (ListView) findViewById(R.id.menu_lateral);

    menuLateral.setAdapter(new MenuAdapter(this));
    menuLateral.setOnItemClickListener(new DrawerItemClickListener(this));
}



private class MenuAdapter extends BaseAdapter {
    private Context context;
    private String[] itensMenu;

    public MenuAdapter(Context context) {
        this.context   = context;
        this.itensMenu = context.getResources().getStringArray(R.array.itens_menu_array);
    }

    @Override
    public int getCount() {
        return itensMenu.length;
    }

    @Override
    public Object getItem(int position) {
        return itensMenu[position];
    }

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

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

        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.item_menu_lateral, parent, false);
        } else {
            row = convertView;
        }

        TextView title  = (TextView) row.findViewById(R.id.menu_item);

        title.setText(itensMenu[position]);
        title.setTypeface(TypeFaces.get(context, "fontawesome-webfont.ttf"));

        return row;
    }
}

根据要求粘贴我的布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context="br.com.impulsoti.transportadornet.activities.MotoristaAutonomoHome">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main" />

<ListView
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/menu_lateral"
    android:background="@drawable/main_background"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:choiceMode="singleChoice" />

这就是追踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.impulsoti.transportadornet/br.com.impulsoti.transportadornet.activities.MotoristaAutonomoHome}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
        at br.com.impulsoti.transportadornet.activities.MotoristaAutonomoHome.onCreate(MotoristaAutonomoHome.java:38)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

- &GT;第38行是menuLateral.setAdapter(new MenuAdapter(this));

我不知道为什么我会得到这个NullPointer,因为适配器看起来正确并且调试不会在我的结尾处抛出错误。

任何帮助?

0 个答案:

没有答案