java.lang.RuntimeException:无法启动活动ComponentInfo添加recyclelerview时

时间:2016-08-18 14:33:45

标签: java android android-studio android-recyclerview

我添加了recyclerview,然后当我运行应用程序时,它停止了。

1)MainActivity.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    recyclerView = (RecyclerView) findViewById(R.id.card_relative_1_9);

    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    adapter = new RecyclerAdapter();
    recyclerView.setAdapter(adapter);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    android.app.FragmentManager fm = getFragmentManager();
    fm.beginTransaction().add(R.id.content_frame, new about_fragment()).commit();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    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);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    android.app.FragmentManager fn = getFragmentManager();
    int id = item.getItemId();

    if (id == R.id.nav_9) {
          fn.beginTransaction().replace(R.id.content_frame, new first_class_9()).commit();

    } else if (id == R.id.nav_10) {
        fn.beginTransaction().replace(R.id.content_frame, new first_class_10()).commit();

    } else if (id == R.id.nav_11) {
        fn.beginTransaction().replace(R.id.content_frame, new first_class_11()).commit();

    } else if (id == R.id.nav_12) {
        fn.beginTransaction().replace(R.id.content_frame, new first_class_12()).commit();

    } else if (id == R.id.nav_aboutUs) {
        fn.beginTransaction().replace(R.id.content_frame, new about_fragment()).commit();

    } else if (id == R.id.nav_feedback) {
        fn.beginTransaction().replace(R.id.content_frame, new feedback_fragment()).commit();

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


}

2)RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private String[] SubTxt = {"NCERT Solution",
            "Notes",
            "Sample Papers"};


    private int[] SubImage = { R.drawable.ic_answers_black_48dp,
            R.drawable.ic_notes_black_48dp,
            R.drawable.ic_sample_papers_black_48dp};

    class ViewHolder extends RecyclerView.ViewHolder{

        public int currentItem;
        public ImageView itemImage;
        public TextView itemTitle;


        public ViewHolder(View itemView) {
            super(itemView);
            itemImage = (ImageView)itemView.findViewById(R.id.SubImage);
            itemTitle = (TextView)itemView.findViewById(R.id.SubTxt);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    int position = getAdapterPosition();

                    Snackbar.make(v, "Click detected on item " + position,
                            Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recycler_1_9, null);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.itemTitle.setText(SubTxt[i]);
        viewHolder.itemImage.setImageResource(SubImage[i]);
    }

    @Override
    public int getItemCount() {
        return SubTxt.length;
    }
}

3)recycler_1_9.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/SubImage"
        android:padding="10dp"
        android:src="@drawable/ic_answers_black_48dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NAME"
        android:id="@+id/SubTxt"
        android:padding="10dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/SubImage"/>


</RelativeLayout>

4)first_class_9.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_nine_page_first">



 <android.support.v7.widget.RecyclerView


android:id="@+id/recycler_nine_page_first"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
class="android.support.v7.widget.RecyclerView"/>
</LinearLayout>

MyProject的

https://drive.google.com/file/d/0B_YQAdQ2qIfiYjI2R2JTWU4yQVE/view?usp=sharing

0 个答案:

没有答案