-1

I am trying to make multiple buttons which open different URLs and I keep getting the same error stating onCreate2(android.os.Bundle) is never used.

Does anyone know how to solve the error and what I can do to stop it occurring in the future,

Thanks

package saintbedeslytham.saintbedes;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;

public class news extends ActionBarActivity {

    Button button1;

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

        button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
    startActivity(NameOfTheIntent);
}
});
        }

Button button2;

    protected void onCreate2(Bundle savedInstanceState) {  **<- Errors here**
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);

        button2 = (Button) findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
                startActivity(NameOfTheIntent);
            }
        });
    }
}
Dalton
  • 19
  • 1
  • 7
  • `onCreate` is a protected method that is called by the Activity lifecycle, and it is called automatically by Android. `onCreate2` is not called anywhere (neither by your code, nor by Android). To solve this, put the `button2` code lines inside `onCreate`, delete the `onCreate2` method and you should be good – walljam7 Mar 09 '15 at 19:29

1 Answers1

0

The problem is no one is going to call onCreate2.

Like Pokas said, onCreate is a method that get call by the system when your Android App started. The system is going to call this method once when right after you press the Icon in the launcher to start your app. It is meant to put set up code for setting up your app.

For more info about life cycle please look at this:http://developer.android.com/training/basics/activity-lifecycle/starting.html

To solve this problem you are facing, you should either call onCreate2 somewhere(not suggested), or you move all your code into onCreate, which should look something like this:

package saintbedeslytham.saintbedes;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;

public class news extends ActionBarActivity {

Button button1;
Button button2;


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

    button1 = (Button) findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=742/Christmas+Newsletter+2014.pdf"));
            startActivity(NameOfTheIntent);
        }
    });


    button2 = (Button) findViewById(R.id.button2);

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent NameOfTheIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.st-bedes-high.lancsngfl.ac.uk/getfile.php?src=737/Autumn+Newsletter+2014.pdf"));
            startActivity(NameOfTheIntent);
        }
    });
}
}
kaho
  • 4,746
  • 1
  • 16
  • 24