-1

I created a scrolling activity but when I debug the app and click on the button to open the activity, I get an error. Basically, it crashes the whole app. However, here's my code below.

Is it because it's a scrolling activity and not an empty activity?

Here is the java file:

news.java

package com.example.it5.foothillers;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class news extends AppCompatActivity {

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

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
           }
       });
   }
}

main_acitivity.java

package com.example.it5.foothillers;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

   Button button;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
           button = (Button)findViewById(R.id.button);
           button.setOnClickListener(this);

   }

   private void buttonClick() {
       startActivity(new Intent("it5.foothillers.news"));
   }

   @Override
   public void onClick(View v) {
       switch (v.getId()){
           case R.id.button:
               buttonClick();
               break;

       }

   }

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.it5.foothillers">

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">

       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

       <activity android:name=".news">
           <intent-filter>
               <action android:name="it5.foothillers.news" />
               <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>
       </activity>

   </application>

</manifest>

Logs:

03-30 12:01:49.871 2122-2122/com.example.it5.foothillers E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.it5.foothillers, PID: 2122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it5.foothillers/com.example.it5.foothillers.news}: 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: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.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.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:197)
   at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
   at com.example.it5.foothillers.news.onCreate(news.java:17)
   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)
Swati Garg
  • 995
  • 1
  • 10
  • 21

3 Answers3

0

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

Mainifest.xml

<activity android:name="com.example.it5.foothillers.news" ></activity>

ZR Low
  • 51
  • 7
0

This Activity already has an action bar supplied by the window decor.

Your exception message is clearly telling you that you already have a Toolbar as part of the Activity's theme.

You should set a theme like this in the styles.xml file if you want to call setSupportActionBar.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

You could probably also just remove these two lines

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

And replace with

ActionBar toolbar = getSupportActionBar();
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

The easiest way to start an activity, is change the intent to this:

startActivity(new Intent(MainActivity.this, news.class));

The stack trace shows you're attempting to add another ActionBar and you already had one provided by the application's theme.

So, you can change the theme at yout activity declaration on manifest to not use an ActionBar, like that:

   <activity android:name=".news"
             android:theme="@style/AppTheme.NoActionBar">
       <intent-filter>
           <action android:name="it5.foothillers.news" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
   </activity>

Or you just remove the toolbar from your activity code and your activity_news.xml (because you're not using it later):

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_news);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
           }
       });