0

I am making button in ActionBar which would open me another class.. This is my MainActivity code:

package com.example.menu;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button add = (Button) findViewById(R.id.add);
        Button mest = (Button) findViewById(R.id.miskaste);

        ActionBar actionBar = getActionBar();
        String dateString = (String) android.text.format.DateFormat.format("yyyy/MM/dd", new java.util.Date());
        actionBar.setTitle(dateString);

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

        add.setOnClickListener(new View.OnClickListener() {

              @Override
              public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Add.class);
                startActivity(intent);
              }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the myouare going to die alone with enu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;



}
}

And this is my Add class:

package com.example.menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Add extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);

        Button orderButton = (Button) findViewById(R.id.but);

        orderButton.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {
            finish();
          }

        });
    }

}

And Manifest:

<activity android:name=".Add" />

My plan as you see in code is to press the button and run another activity but when I run my app - it crashes. So can someone find mistake I have worked for this for two hours and nothing...

And this is my Logcat:

05-21 11:57:39.596: E/AndroidRuntime(622):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.os.Looper.loop(Looper.java:137)
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 11:57:39.596: E/AndroidRuntime(622):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:57:39.596: E/AndroidRuntime(622):  at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:57:39.596: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 11:57:39.596: E/AndroidRuntime(622):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 11:57:39.596: E/AndroidRuntime(622):  at dalvik.system.NativeStart.main(Native Method)
05-21 11:57:39.596: E/AndroidRuntime(622): Caused by: java.lang.NullPointerException
05-21 11:57:39.596: E/AndroidRuntime(622):  at com.example.menu.MainActivity.onCreate(MainActivity.java:27)
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.app.Activity.performCreate(Activity.java:4465)
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 11:57:39.596: E/AndroidRuntime(622):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
Naveen Kumar
  • 3,738
  • 4
  • 29
  • 50
AndroidFreak
  • 866
  • 1
  • 10
  • 31

3 Answers3

0

If You want to start this Add Activity You must add in AndroidManifest.xml this intent filter for that Add activity:

<intent-filter>
   <action android:name="android.intent.action.MAIN" />
</intent-filter>
kselos
  • 24
  • 1
0

Assuming that you are trying to call the button in actionbar as you mentioned

i need it to be in actionbar

and you have mentioned your option menu item as

    <item android:id="@+id/add" 
android:icon="@drawable/add" 
android:showAsAction="ifRoom|withText" />

try calling this in your code

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.add:
      Intent intent = new Intent(MainActivity.this, Add.class);
                startActivity(intent);
      break;  

    default:     

      break;
    }

    return true;
  }
Oam
  • 912
  • 6
  • 7
-1

there is a file activity_main in youre 'layout'-folder, there is where u should create youre button like so:

<Button
            android:id="@+id/btn_next"
            style="@style/CameraButtonText"
            android:layout_marginRight="5dp"
            android:background="@layout/nextbutton"
            android:onClick="overviewClick"
            android:text="@string/next" />

and u have to inflate it if u want it in actionbar: http://developer.android.com/guide/topics/ui/actionbar.html

everything can be found in the link