我的导航抽屉里有一个RecyclerView。我一直得到一个空指针异常。我看过类似的问题,但似乎没有一个解决方案。请帮忙。我不知道问题是什么。 Gradle具有所有必需的依赖项。
activity_expert.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout">
<!--Content Section-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainContent">
<include layout="@layout/toolbar"></include>
<!--Main Content-->
<android.support.v7.widget.RecyclerView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/mainContentView">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
<!--Drawer section-->
<android.support.v7.widget.RecyclerView
android:layout_width="240dp"
android:layout_height="wrap_content"
android:id="@+id/drawer_recycler_view"
android:layout_gravity="start"
android:background="@color/white"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
drawer_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/slider_3_small">
</RelativeLayout>
drawer_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rowIcon"
android:paddingLeft="16dp"
android:src="@drawable/cow_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="12dp"
android:paddingTop="4dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/rowText"
/>
</LinearLayout>
ExpertActivity.java
package com.lavineoluoch.myapplication.cowsoko;
import android.app.ActionBar;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import com.lavineoluoch.myapplication.cowsoko.appUtils.DrawerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ExpertActivity extends AppCompatActivity {
//Data used to populate RecyclerView
String TITLES[] = {"Buy a Cow","Meet an Expert","Model Farms","Share","Rate","Settings"};
int ICONS[] = {R.drawable.cow_icon,R.drawable.expert_icon,R.drawable.farm_icon,R.drawable.share,R.drawable.rate,R.drawable.settings};
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expert);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
recyclerView = (RecyclerView)findViewById(R.id.drawer_recycler_view);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.app_name,R.string.app_name);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
DrawerAdapter drawerAdapter = new DrawerAdapter(TITLES,ICONS);
recyclerView.setAdapter(drawerAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Meet an Expert");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_expert, menu);
//Insert Search View into toolbar
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
最后是DrawerAdapter.java
package com.lavineoluoch.myapplication.cowsoko.appUtils;
import android.provider.Telephony;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.lavineoluoch.myapplication.cowsoko.R;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
/**
* Created by USER on 8/7/2015.
*
* This class is the RecyclerView Adapter
*/
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.ViewHolder> {
private static final int HEADER_TYPE = 0;
private static final int ITEM_TYPE = 1;
private String NavTitles[];
private int NavIcons[];
public DrawerAdapter(String NavTitles[],int NavIcons[]) {
this.NavTitles = NavTitles;
this.NavIcons = NavIcons;
}
//Called whenever a new instance of our ViewHolder class is created
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_row,parent,false);
ViewHolder viewHolderItem = new ViewHolder(v,viewType);
return viewHolderItem;
}else if (viewType == HEADER_TYPE){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_header,parent,false);
ViewHolder viewHolderHeader = new ViewHolder(v,viewType);
return viewHolderHeader;
}
return null;
}
//Called when the SO binds the view with the data -- or, in other words, the data is shown in the UI
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (holder.holderId == 1){
holder.textView.setText(NavTitles[position - 1]);
holder.imageView.setImageResource(NavIcons[position - 1]);
}
}
@Override
public int getItemCount() {
return NavTitles.length + 1; }
@Override
public int getItemViewType(int position) {
if (position == 0)
return HEADER_TYPE;
return ITEM_TYPE;
}
/**
* A ViewHolder describes an item view and metadata about its place within the RecyclerView.
* RecyclerView.Adapter implementations should subclass ViewHolder and add fields for
* caching potentially expensive findViewById(int) results.
*
*/
public static class ViewHolder extends RecyclerView.ViewHolder{
protected int holderId;
@InjectView(R.id.rowIcon)ImageView imageView;
@InjectView(R.id.rowText)TextView textView;
/**
*
* @param itemView
* @param viewType
*/
public ViewHolder(View itemView,int viewType) {
super(itemView);
this.holderId = viewType;
if (viewType == ITEM_TYPE){
ButterKnife.inject(this,itemView);
holderId = 1;
}else {
holderId = 0;
}
}
}
}
我的logcat看起来像这样
08-08 19:26:35.847 18694-18694 / com.lavineoluoch.myapplication.cowsoko E / AndroidRuntime:FATAL EXCEPTION:main 显示java.lang.NullPointerException 在android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764) 在android.view.View.measure(View.java:16030) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 在android.view.View.measure(View.java:16030) 在android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868) 在android.view.View.measure(View.java:16030)
答案 0 :(得分:0)
我使用此代码来解决此问题:
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.nav_header, mNavigationView);