FindViewById()返回null而没有编译错误

时间:2015-01-09 23:37:24

标签: android listview navigation-drawer baseadapter

当我尝试运行我的应用程序时,应用程序崩溃并且logcat给出了Nullpointer:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

有人可以帮忙吗?

我正在尝试在我的导航栏中放置一个带有自定义适配器的列表视图。

我认为问题是由于尝试将适配器绑定到listview引起的。 提前谢谢。

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

    listView = (ListView) findViewById(R.id.listView1);
    myAdapter = new MyAdapter(this);
    listView.setAdapter(myAdapter);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    drawerFragment.setUp((R.id.fragment_navigation_drawer), (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

class MyAdapter extends BaseAdapter{
    private Context context;
    String[] socialSites;
    int[] images = {R.drawable.ic_1, R.drawable.ic_2, R.drawable.ic_3, R.drawable.ic_4, R.drawable.ic_5};
    public MyAdapter (Context context){
        this.context=context;
        socialSites=context.getResources().getStringArray(R.array.social);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row =null;
        if(convertView==null){
            LayoutInflater inflator= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflator.inflate(R.layout.cutom_row, parent, false);
        }
        else{
            row=convertView;
        }
        TextView titleTextView = (TextView) row.findViewById(R.id.textView);
        ImageView titleImageView = (ImageView) row.findViewById(R.id.imageView);
        titleTextView.setText(socialSites[position]);
        titleImageView.setImageResource(images[position]);

        return row;
    }
}

编辑:

<android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="android.getgo.org.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/app_bar"
        android:text="@string/hello_world" />

</RelativeLayout>

<fragment
    android:id="@+id/fragment_navigation_drawer"
    android:name="android.getgo.org.NavigationDrawerFragment"
    android:layout_width="@dimen/nav_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1" />

</fragment>
</android.support.v4.widget.DrawerLayout>

编辑:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}

2 个答案:

答案 0 :(得分:0)

在setAdapter

之前进行listview初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_appbar);

    listView = (ListView) findViewById(R.id.listView1);

    myAdapter = new MyAdapter(this);
    listView.setAdapter(myAdapter);

    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

    drawerFragment.setUp((R.id.fragment_navigation_drawer), (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

答案 1 :(得分:0)

创建一个新的布局文件并将其命名为fragment_navigation_drawer:

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1" />
</RelativeLayout>

相应地更新您的OnCreateView:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_navigation_drawer,null);

        ListView listView = view.findViewById(R.id.listView1);
        MyAdapter  myAdapter = new MyAdapter(getActivity());
        listView.setAdapter(myAdapter);
        return view;
    }

您应该将MyAdapter代码移动到单独的类中,或将其设置为静态并将其导入到片段中。