0

Maybe it's a simple question but I can't get the answer, although I searched for hours and more in google, etc.

I set up two activities and two layouts - main(background white), second(background black).
Then I set up a new resource file in the layout folder called toolbar:

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black">

<Button
    android:id="@+id/profile_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="start"
    android:text="Profile" />

</android.support.v7.widget.Toolbar>

My question now is:
Where do I put the method to start the second activity by pressing the button in my toolbar? and how do I do it?
Tried it with onClick in the XML file and a method in the MainActivity OR with a OnClickListenerMethod in the MainActivity class.

I have this:

public void start(View view) { 
    Intent intent = new Intent(this, ProfileActivity.class);  
    startActivity(intent); 
} 

But I got errors and my App shut down: NullPointerException and IllegalStateException

Hope you guys will help me, cause I just started with Java!

Edit: The only method in main and second activity is the onCreate Method with super.onCreate and setContentView. Maybe I have to add something?

Here is the error:  

05-02 23:48:23.533 17050-17050/com.example.timomichel.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.timomichel.myapplication, PID: 17050
  java.lang.IllegalStateException: Could not execute method for android:onClick
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
      at android.view.View.performClick(View.java:5198)
      at android.view.View$PerformClick.run(View.java:21147)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invoke(Native Method)
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
      at android.view.View.performClick(View.java:5198) 
      at android.view.View$PerformClick.run(View.java:21147) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
   Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.timomichel.myapplication/com.example.timomichel.myapplication.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
      at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1794)
      at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
      at android.app.Activity.startActivityForResult(Activity.java:3917)
      at android.app.Activity.startActivityForResult(Activity.java:3877)
      at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
      at android.app.Activity.startActivity(Activity.java:4200)
      at android.app.Activity.startActivity(Activity.java:4168)
      at com.example.timomichel.myapplication.MainActivity.start(MainActivity.java:18)
      at java.lang.reflect.Method.invoke(Native Method) 
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
      at android.view.View.performClick(View.java:5198) 
      at android.view.View$PerformClick.run(View.java:21147) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:148) 
      at android.app.ActivityThread.main(ActivityThread.java:5417) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Timo
  • 21
  • 4
  • 1
    Maybe you should show the Java code for `findViewById(R.id.profile_button)` – OneCricketeer May 02 '16 at 21:29
  • But, adding buttons like that seems wrong. If you are trying to add action menu buttons you can [follow the documentation](https://developer.android.com/training/appbar/actions.html). – OneCricketeer May 02 '16 at 21:32
  • sorry but i forget to add the `android:onClick="start" in the XML file`. So i have this Method: `public void start(View view) { Intent intent = new Intent(this, ProfileActivity.class); startActivity(intent); }` – Timo May 02 '16 at 21:33
  • There is an [edit] link under the tags of your question. – OneCricketeer May 02 '16 at 21:36
  • What you've mentioned should work. Can you please share the [logcat](http://stackoverflow.com/a/23353174/2308683)? – OneCricketeer May 02 '16 at 21:39
  • Sorry, next time i will use the edit, yeah but i messed something, I'm setting up a new project, maybe it will help if i say, that i want a toolbar like in whatsapp or Instagram to switch to settings, etc.? – Timo May 02 '16 at 21:44
  • That's an overflow menu, so you need a `res/menu` resource instead of a Button. The second comment where I linked to the documentation does what you want. – OneCricketeer May 02 '16 at 21:48
  • Okay, then i will read through this, thanks a lot! – Timo May 02 '16 at 21:49
  • I also added the log – Timo May 02 '16 at 21:52
  • Read the error please. `SecondActivity... have you declared this activity in your AndroidManifest.xml?` – OneCricketeer May 02 '16 at 21:55
  • Oh no, i did this with 'new -> class/layout resource field' in this project, that could be my problem. But this was the wrong approach anyway, so i read through your link and thanks a lot! :) – Timo May 02 '16 at 21:59

1 Answers1

0

Declare SecondActivity in your AndroidManifest.xml:

<activity android:name="com.example.timomichel.myapplication.SecondActivity">
    ....
</activity>
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71