FindViewById() returns null 没有编译错误
FindViewById() returns null without compilation error
当我尝试 运行 我的应用程序时,应用程序崩溃并且 logcat 给出一个空指针:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
有人可以帮忙吗?
我正在尝试将带有自定义适配器的列表视图放入我的导航抽屉中。
我认为问题是由于试图将适配器绑定到列表视图引起的。
提前谢谢你。
@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);
}
将listview初始化放在setAdapter之前
@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);
}
创建一个新的布局文件并命名为 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 代码移动到一个单独的 class 或将其设为静态并将其导入您的片段。
当我尝试 运行 我的应用程序时,应用程序崩溃并且 logcat 给出一个空指针:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
有人可以帮忙吗?
我正在尝试将带有自定义适配器的列表视图放入我的导航抽屉中。
我认为问题是由于试图将适配器绑定到列表视图引起的。 提前谢谢你。
@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);
}
将listview初始化放在setAdapter之前
@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);
}
创建一个新的布局文件并命名为 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 代码移动到一个单独的 class 或将其设为静态并将其导入您的片段。