J'essaie d'implémenter la disposition des onglets coulissants à l'aide de la conception de matériel Android. Mais cela me donne NullPointerException
. Voici mon code jusqu'à présent:
MainActivity.Java
public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager pager;
private SlidingTabLayout nLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer_fragment);
drawerFragment.setUp(R.id.navigation_drawer_fragment, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
nLayout = (SlidingTabLayout) findViewById(R.id.tabs);
nLayout.setViewPager(pager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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;
}
if (id == R.id.navigate) {
startActivity(new Intent(this, SubActivity.class));
}
return super.onOptionsItemSelected(item);
}
class MyPagerAdapter extends FragmentPagerAdapter {
String tabs[];
public MyPagerAdapter(FragmentManager fm) {
super(fm);
tabs = getResources().getStringArray(R.array.tabs);
}
@Override
public Fragment getItem(int position) {
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 3;
}
}
public static class MyFragment extends Fragment {
private TextView textView;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_my, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundle = getArguments();
if (bundle != null) {
textView.setText("The page selected is " + bundle.getInt("position"));
}
return layout;
}
}
}
activity_main.xml
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical"
tools:context="carsaleapp.shanaka.com.carsaleapp.MainActivity">
<include
Android:id="@+id/app_bar"
layout="@layout/app_bar" />
<app.shanaka.tabs.SlidingTabLayout
Android:id="@+id/tabs"
Android:layout_width="match_parent"
Android:layout_height="wrap_content" />
<Android.support.v4.view.ViewPager
Android:id="@+id/pager"
Android:layout_width="match_parent"
Android:layout_height="0dp"
Android:layout_weight="1" />
</LinearLayout>
Voici l'erreur que j'ai eu après avoir exécuté mon application:
Java.lang.RuntimeException: Unable to start activity ComponentInfo{carsaleapp.shanaka.com.carsaleapp/carsaleapp.shanaka.com.carsaleapp.MainActivity}: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v4.view.ViewPager.setAdapter(Android.support.v4.view.PagerAdapter)' on a null object reference
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2298)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
Caused by: Java.lang.NullPointerException: Attempt to invoke virtual method 'void Android.support.v4.view.ViewPager.setAdapter(Android.support.v4.view.PagerAdapter)' on a null object reference
at carsaleapp.shanaka.com.carsaleapp.MainActivity.onCreate(MainActivity.Java:42)
at Android.app.Activity.performCreate(Activity.Java:5933)
at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1105)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2251)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2360)
at Android.app.ActivityThread.access$800(ActivityThread.Java:144)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1278)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:135)
at Android.app.ActivityThread.main(ActivityThread.Java:5221)
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:899)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:694)
Ils continuent à dire que l'objet pager
est null. Mais pour moi je l'ai réglé correctement.
pager = (ViewPager) findViewById(R.id.pager);
Vous avez votre page de vue dans activity_main.xml
alors que votre mise en page pour l'activité est setContentView(R.layout.activity_main_appbar)
. Vous n'avez pas de pageur de vue dans activity_main_appbar.xml
.
findViewById
recherche une vue dans la hiérarchie de vues actuelle. Je suppose que cela devrait être setContentView(R.layout.activity_main);
mais vérifiez les vues que vous avez dans la disposition incluse app_bar.xml
Il se peut que vous passiez un mauvais identifiant de viewPager.Veuillez vérifier votre identifiant à partir d'un fichier XML