0

I have 2 activity in my app - first and second. Currenly my default is first.

        <activity
        android:name=".FirstActivity"
        android:label="randomlabel"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Now i want to change default activity so i change like this:

        <activity
        android:name=".FirstActivity"
        android:label="randomlabel"
        >

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

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

and app now didn't work, what is bad here ?

error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.russian.app/ru.russian.app.FirstActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Edit2: FirstActivity

public class FirstActivity extends AppCompatActivity {

    Button button10;

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

        button10.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                goToSecond();
            }
        });


    }


    private void goToSecond() {

        Intent intent = new Intent(this, SecondActivity.class);

        startActivity(intent);

    }

}
lukash
  • 3
  • 4

3 Answers3

1

It looks like from your stacktrace that you forgot to bind the button in FirstActivity. You need to bind it by calling

button10 = (Button) findViewById(R.id.my_button_id);

Replace my_button_id with the id that you set for the button in your activity_first layout.

lithiumsheep
  • 254
  • 2
  • 13
0

Try this:

 <activity
        android:name=".FirstActivity"
        android:label="randomlabel">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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


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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity
EtherPaul
  • 424
  • 10
  • 19
  • I think he has a different error, because it is not necessary to specify any intent-filter for Activities in order to swith between them. Only adding it to AndroidManifest should be sufficient. – David Kasabji Jan 11 '17 at 21:12
  • I understand but because he didn't post any error messages I try to help in any way possible... – EtherPaul Jan 11 '17 at 21:17
0

it looks like you are trying to set a onClick() listener in your activity on a view that hasn't been initialised correctly.

you need to initialise the button from the inflated view.

add

button10 = (Button) findViewById(R.id.button10);
Matthew Shearer
  • 2,715
  • 3
  • 23
  • 32
  • i did edit2 wtih First activity, second activity is about 2000 lines, too much to put here, second activity is mapsactivity – lukash Jan 11 '17 at 21:56
  • wow, thanks, i sat too many hours in a row in android studio, this is the basis! – lukash Jan 11 '17 at 23:51
  • no problem, try starting from some android development tutorials. Don't rush into creating your app straight away – Matthew Shearer Jan 11 '17 at 23:52