2016-05-27 73 views
0

我正在使用各種教程製作應用程序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)  

所以我做了什麼與 「setHasFixedSize(布爾)」 撥錯? 如果您需要更多資源(佈局,活動),請在評論中告訴我。

編輯

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是正確的......

謝謝

+0

你可以顯示你的XML佈局? – Masum

+0

您嘗試實例化'R.id.my_recycler_view'的RecyclerView不在'R.layout.activity_main' XML佈局中 – mastrgamr

回答

0

所以,我錯過了我的佈局不在setContentView中。就這樣。無論如何感謝您的幫助。

0

的ID my_recycler_view不正確,你能分享這是可能的與我們聯繫activity_main.xml