我的導航抽屜中有一個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:致命例外:main java.lang.NullPointer異常 at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:1764) at android.view.View.measure(View.java:16030) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java :5012) at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) at android.view.View.measure(View.java:16030) at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout的.java:868) 在android.view.View.measure(View.java:16030)
你作爲mNavigationView分配了什麼?這似乎是我需要的,但我不完全確定這個視圖組是如何建立的。 – justinraczak