Alles was ich versuche zu tun ist eine recyclerview innerhalb eines tablayout zu erstellen Ich habe versucht, den Adapter in der Hauptaktivität sowie das Fragment im Einsatz in der tablayout aber egal wie bekomme ich immer noch diesen FehlerNullpointerexception on RecyclerView
java.lang.RuntimeException: Aktivität kann nicht gestartet werden ComponentInfo {knightsrealms.managment_app/knightsrealms.managment_app.Dashboard}: java.lang.NullPointerException: Versuch, die virtuelle Methode 'void android.support.v7.widget.RecyclerView.setAdapter (android.support.v7.widget.RecyclerView $ Adapter)‘auf einer null-Objekt Referenz
package knightsrealms.managment_app;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import java.util.ArrayList;
import java.util.Calendar;
public class Dashboard extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ViewPageAdapter viewPagerAdapter;
ArrayList<knightsrealms.managment_app.Calendar> contacts = new ArrayList<knightsrealms.managment_app.Calendar>(5);
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
viewPagerAdapter = new ViewPageAdapter(getSupportFragmentManager());
viewPager.setAdapter(viewPagerAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
setSupportActionBar(toolbar);
final TabLayout.Tab messages = tabLayout.newTab();
final TabLayout.Tab dashboard = tabLayout.newTab();
messages.setText("Messages");
dashboard.setText("Dashboard");
tabLayout.addTab(dashboard, 0);
tabLayout.addTab(messages, 1);
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));
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) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
RecyclerView rvContacts = (RecyclerView)findViewById(R.id.rvcal);
contacts.add(0, new knightsrealms.managment_app.Calendar("Number 1",true));
contacts.add(1, new knightsrealms.managment_app.Calendar("Number 2",true));
CalendarAdapter adapter = new CalendarAdapter(contacts);
rvContacts.setAdapter(adapter);
rvContacts.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dashboard_menu, menu);
return true;
}
void selectPage(int pageIndex){
tabLayout.setScrollPosition(pageIndex,0f,true);
viewPager.setCurrentItem(pageIndex);
}
}
dies ist mein Adapter
package knightsrealms.managment_app;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.List;
public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.ViewHolder> {
// Provide a direct reference to each of the views within a data item
// Used to cache the views within the item layout for fast access
public static class ViewHolder extends RecyclerView.ViewHolder {
// Your holder should contain a member variable
// for any view that will be set as you render a row
public TextView nameTextView;
public Button messageButton;
// We also create a constructor that accepts the entire item row
// and does the view lookups to find each subview
public ViewHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
messageButton = (Button) itemView.findViewById(R.id.message_button);
}
}
private List<Calendar> mContacts;
// Pass in the contact array into the constructor
public CalendarAdapter(List<Calendar> contacts) {
mContacts = contacts;
}
// Usually involves inflating a layout from XML and returning the holder
@Override
public CalendarAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.recyclercell, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(CalendarAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Calendar contact = mContacts.get(position);
// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(contact.getName());
Button button = viewHolder.messageButton;
if (contact.isOnline()) {
button.setText("Message");
button.setEnabled(true);
}
else {
button.setText("Offline");
button.setEnabled(false);
}
}
// Return the total count of items
@Override
public int getItemCount() {
return mContacts.size();
}
}
Dieser Code für das Fragment ist, das innerhalb des tablayout ist
<android.support.v7.widget.RecyclerView
android:id="@+id/rvcal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Und das ist die Haupttätigkeit xml
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"
></include>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:layout_below="@+id/tool_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:tabIndicatorHeight="3dp"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
/>
</RelativeLayout>
jede Einsicht/Hilfe in das wird sehr geschätzt danke !!
diese Zeile 'viewPager.setAdapter (viewPagerAdapter);' erzeugt die Fehlerursache viewPager ist null . Bitte teilen Sie Ihre XML-Datei, damit wir sehen können, was Sie dort gemacht haben und wo der Fehler liegt. – Vucko
@Vucko Ich habe gerade die Frage mit dem XML-Code – XvKnightvX
aktualisiert. Sie sagen, dass diese XML-Datei 'main_activity.xml' ist, aber in Ihrer 'Dashboard'-Aktivität tun Sie' setContentView (R.layout.activity_dashboard); '. Ich denke, du versuchst vielleicht, mit falscher Aktivität auf den Viewpager zuzugreifen. Kannst du das überprüfen? Was ist MainActivity und was ist Dasboard? – Vucko