2016-05-27 14 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(ブール値)" とworng行って何? もっとリソース(レイアウト、アクティビティ)をもっと欲しがったらコメントで教えてください。

EDIT

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のmy_recycler_viewが正しくない可能性がありますあなたの

+0

xmlレイアウトを表示できますか? – Masum

+0

'R.id.my_recycler_view'をインスタンス化しようとしたRecyclerViewは' R.layout.activity_main' XMLレイアウトにありません – mastrgamr

答えて

0

activity_main.xmlたちで、私はその私の見逃していますレイアウトはsetContentViewにありません。それで全部です。とにかく助けてくれてありがとう。

0

ありがとう... idが正しいようですだから、

関連する問題