我的recyclerView

时间:2016-05-27 15:11:28

标签: android crash android-recyclerview

我通过各种教程使用RecyclerView制作应用程序。现在应用程序崩溃了。这是我的MainActivity:

import android.os.*;
import android.support.design.widget.*;
import android.support.v4.view.*;
import android.support.v7.app.*;
import android.support.v7.widget.*;
import android.util.*;
import android.view.*;
import java.util.*;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "CardViewActivity";


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

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

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.addTab(tabLayout.newTab().setText("Planes"));
        tabLayout.addTab(tabLayout.newTab().setText("Solids"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        final PagerAdapter adapter = new ViewPagerAdapter
        (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);

        // Code to Add an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);

        // Code to remove an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).deleteItem(index);
    }

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

    }
    @Override
    protected void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter
            .MyClickListener() {
                @Override
                public void onItemClick(int position, View v) {
                    Log.i(LOG_TAG, " Clicked on Item " + position);
                }
            });
    }

    private ArrayList<PlanesData> getDataSet() {
        ArrayList results = new ArrayList<PlanesData>();
        for (int index = 0; index < 20; index++) {
            PlanesData obj = new PlanesData("Some Primary Text " + index,
                                            "Secondary " + index);
            results.add(index, obj);
        }
        return results;
    }
}

这是logcat的一部分,错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.im.shapes, PID: 2979
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.im.shapes/com.im.shapes.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                                                                 at com.im.shapes.MainActivity.onCreate(MainActivity.java:54)
                                                                 at android.app.Activity.performCreate(Activity.java:5990)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

所以我用它做了什么&#34; setHasFixedSize(boolean)&#34;? 如果您需要更多资源(布局,活动),请在评论中告诉我。

修改

activity_main.xml中:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"/>

</RelativeLayout>

看来id是正确的......

谢谢

2 个答案:

答案 0 :(得分:0)

id my_recycler_view可能不正确,您可以与我们分享activity_main.xml

答案 1 :(得分:0)

所以,我错过了我的布局不在setContentView中。就这样。无论如何,谢谢你的帮助。