-1

我的應用程序在我的recyclerView中設置適配器時出錯。我實現了所有的方法,看看我的java類適配器的地方,但我不知道我的錯誤在哪裏。我甚至重寫我的代碼,但每次運行應用程序時仍然會出現相同的錯誤。 logcat把我帶到我的MainActivity.java,在那裏我設置我的適配器。看一看。在RecyclerView和Cardview Android Studio中設置適配器的錯誤

MainActivity.java

public class MainActivity extends AppCompatActivity { 

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


     List<Data_Reminder> data = fill_with_data(); 

     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.reminder_recyclerview); 
     recycler_reminder adapter = new recycler_reminder(data, getApplication()); 
     recyclerView.setAdapter(adapter); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
} 

public List<Data_Reminder> fill_with_data() { 

     List<Data_Reminder> data = new ArrayList<>(); 

     data.add(new Data_Reminder("Batman vs Superman", "Following the destruction of Metropolis, Batman embarks on a personal vendetta against Superman ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("X-Men: Apocalypse", "X-Men: Apocalypse is an upcoming American superhero film based on the X-Men characters that appear in Marvel Comics ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Captain America: Civil War", "A feud between Captain America and Iron Man leaves the Avengers in turmoil. ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Kung Fu Panda 3", "After reuniting with his long-lost father, Po must train a village of pandas", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Warcraft", "Fleeing their dying home to colonize another, fearsome orc warriors invade the peaceful realm of Azeroth. ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Alice in Wonderland", "Alice in Wonderland: Through the Looking Glass ", R.drawable.ic_action_movie)); 

     return data; 
    } 
} 

fragment_blank.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="layout.BlankFragment"> 

    <!-- TODO: Update blank fragment layout --> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="200dp"> 

     <android.support.v7.widget.RecyclerView 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/reminder_recyclerview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </ScrollView> 


</FrameLayout> 

Data_Reminder.java

public class Data_Reminder { 
    public String name; 
    public String description; 
    public int imageId; 

    Data_Reminder(String name, String description, int imageId) { 
     this.name = name; 
     this.description = description; 
     this.imageId = imageId; 
    } 

} 

recycler_reminder.java

public class recycler_reminder extends RecyclerView.Adapter<view_holder> { 

    List<Data_Reminder> list = Collections.emptyList(); 
    Context context; 

    public recycler_reminder(List<Data_Reminder> list, Context context) { 
     this.list = list; 
     this.context = context; 
    } 

    @Override 
    public view_holder onCreateViewHolder(ViewGroup parent, int viewType) { 
     //Inflate the layout, initialize the View Holder 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout_reminder, parent, false); 
     view_holder holder = new view_holder(v); 
     return holder; 

    } 

    @Override 
    public void onBindViewHolder(view_holder holder, int position) { 

     //Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView 
     holder.name.setText(list.get(position).name); 
     holder.description.setText(list.get(position).description); 
     holder.imageView.setImageResource(list.get(position).imageId); 

     //animate(holder); 

    } 

    @Override 
    public int getItemCount() { 
     //returns the number of elements the RecyclerView will display 
     return list.size(); 
    } 

    @Override 
    public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
     super.onAttachedToRecyclerView(recyclerView); 
    } 

    // Insert a new item to the RecyclerView on a predefined position 
    public void insert(int position, Data_Reminder data) { 
     list.add(position, data); 
     notifyItemInserted(position); 
    } 

    // Remove a RecyclerView item containing a specified Data object 
    public void remove(Data_Reminder data) { 
     int position = list.indexOf(data); 
     list.remove(position); 
     notifyItemRemoved(position); 
    } 

} 

這裏是我的logcat的

07-12 00:50:54.394 27684-27684/application.cedie.myapplication E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: application.cedie.myapplication, PID: 27684 
                       java.lang.RuntimeException: Unable to start activity ComponentInfo{application.cedie.myapplication/application.cedie.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
                        at android.app.ActivityThread.access$800(ActivityThread.java:147) 
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                        at android.os.Looper.loop(Looper.java:135) 
                        at android.app.ActivityThread.main(ActivityThread.java:5253) 
                        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:949) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:744) 
                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 
                        at application.cedie.myapplication.MainActivity.onCreate(MainActivity.java:65) 
                        at android.app.Activity.performCreate(Activity.java:5975) 
                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) 
                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)  
                        at android.app.ActivityThread.access$800(ActivityThread.java:147)  
                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)  
                        at android.os.Handler.dispatchMessage(Handler.java:102)  
                        at android.os.Looper.loop(Looper.java:135)  
                        at android.app.ActivityThread.main(ActivityThread.java:5253)  
                        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:949)  
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:744)  
+0

張貼堆棧跟蹤 – Raghunandan

+1

設置您的佈局管理之前,您setAdapter –

+0

仍然得到錯誤@馬克敏銳 – RoyalCediee

回答

0

我已經通過移動這個代碼進入我BlankFragment.java解決了這個問題

List<Data_Reminder> data = fill_with_data(); 

     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.reminder_recyclerview); 
     recycler_reminder adapter = new recycler_reminder(data, getApplication()); 
     recyclerView.setAdapter(adapter); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
} 

public List<Data_Reminder> fill_with_data() { 

     List<Data_Reminder> data = new ArrayList<>(); 

     data.add(new Data_Reminder("Batman vs Superman", "Following the destruction of Metropolis, Batman embarks on a personal vendetta against Superman ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("X-Men: Apocalypse", "X-Men: Apocalypse is an upcoming American superhero film based on the X-Men characters that appear in Marvel Comics ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Captain America: Civil War", "A feud between Captain America and Iron Man leaves the Avengers in turmoil. ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Kung Fu Panda 3", "After reuniting with his long-lost father, Po must train a village of pandas", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Warcraft", "Fleeing their dying home to colonize another, fearsome orc warriors invade the peaceful realm of Azeroth. ", R.drawable.ic_action_movie)); 
     data.add(new Data_Reminder("Alice in Wonderland", "Alice in Wonderland: Through the Looking Glass ", R.drawable.ic_action_movie)); 

     return data; 
    } 

這是在onCreateView()方法在我的新BlankFrament.java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    View rootView = inflater.inflate(R.layout.fragment_blank, container, false); 
    // 1. get a reference to recyclerView 
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.reminder_recyclerview); 

    // 2. set layoutManger 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    // 3. create an adapter 
    List<Data_Reminder> data = fill_with_data(); 
    recycler_reminder adapter = new recycler_reminder(data, getActivity()); 
    recyclerView.setAdapter(adapter); 

    return rootView; 
} 

public List<Data_Reminder> fill_with_data() { 

    List<Data_Reminder> data = new ArrayList<>(); 

    data.add(new Data_Reminder("Batman vs Superman", "Following the destruction of Metropolis, Batman embarks on a personal vendetta against Superman ", R.drawable.ic_action_movie)); 
    data.add(new Data_Reminder("X-Men: Apocalypse", "X-Men: Apocalypse is an upcoming American superhero film based on the X-Men characters that appear in Marvel Comics ", R.drawable.ic_action_movie)); 
    data.add(new Data_Reminder("Captain America: Civil War", "A feud between Captain America and Iron Man leaves the Avengers in turmoil. ", R.drawable.ic_action_movie)); 
    data.add(new Data_Reminder("Kung Fu Panda 3", "After reuniting with his long-lost father, Po must train a village of pandas", R.drawable.ic_action_movie)); 
    data.add(new Data_Reminder("Warcraft", "Fleeing their dying home to colonize another, fearsome orc warriors invade the peaceful realm of Azeroth. ", R.drawable.ic_action_movie)); 
    data.add(new Data_Reminder("Alice in Wonderland", "Alice in Wonderland: Through the Looking Glass ", R.drawable.ic_action_movie)); 

    return data; 
}