choi choi - 1 month ago 12
Android Question

Intent from an activity to another containing Tabs

I am trying to access an activity which has 3 tabs i.e. 3 fragments. I passed an intent to open that activity but the app stops as soon as i click it. What am i doing wrong?
here is the intent.

public void Registernow() {

Intent intent = new Intent(this, Payment.class);
startActivity(intent);

}


and here is my code for the activity i am trying to access.

public class Payment extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager); //assigns viewpager to tablayout
}

private void setupViewPager(ViewPager viewPager) { //defines number of tabs by setting appropriate fragment and tab name
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Recharge(), "RECHARGE");
adapter.addFragment(new Bill(), "BILL");
adapter.addFragment(new Transfer(), "TRANSFER");
viewPager.setAdapter(adapter); //custom adapter class provides fragments required for the view pager.
}


}


i am mentioning the error that i got while debugging the app. This happens as soon as i click the button.

/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aadesh.saptrishi, PID: 25817
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aadesh.saptrishi/com.example.aadesh.saptrishi.Payment}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
at android.app.ActivityThread.access$900(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:199)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.aadesh.saptrishi.Payment.onCreate(Payment.java:35)
at android.app.Activity.performCreate(Activity.java:5322)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
at android.app.ActivityThread.access$900(ActivityThread.java:166) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:146) 
at android.app.ActivityThread.main(ActivityThread.java:5511) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
at dalvik.system.NativeStart.main(Native Method) 

Answer

Your application Theme should extend from Theme.AppCompat.NoActionBar in order to use the Toolbar as an ActionBar