zain zorro
zain zorro

Reputation: 45

start a new activity button pressing a button

I want a new activity to start when i press the "createAppointment" button but so far when i click the button, the application crashes.

creation for button:

<Button
    android:id="@+id/crtApp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Create Appointment"
    android:layout_gravity="center"/>

createApp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/createAppointment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="36sp"
        android:text="Create Appointment"
        android:textSize="34sp" />

    <TextView
        android:id="@+id/titleOfAppointment"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:text="Title"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/titleappointment"
        android:layout_width="295dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15sp"
        android:layout_marginLeft="10dp"
        android:ems="10" />

    <TextView
        android:id="@+id/timeappointment"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:text="Time"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/timeappointment"
        android:layout_width="295dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15sp"
        android:layout_marginLeft="10dp"
        android:ems="10" />

    <TextView
        android:id="@+id/detailsappointment"
        android:layout_width="318dp"
        android:layout_height="wrap_content"
        android:text="Details"
        android:textSize="24sp" />
    <EditText
        android:id="@+id/detailsappointment"
        android:layout_width="292dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="65dp"
        android:layout_marginLeft="10dp"
        android:layout_weight="0.07"
         />

     <Button
        android:id="@+id/save_button"
        android:layout_width="105dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_weight="0.01" 
        android:text="Save"/>

</LinearLayout>

CreateAppointment.java

package com.examples;

import android.app.Activity;
import android.os.Bundle;

public class CreateAppointment extends Activity {

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

}

code for the button to start a new activity

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_calendar_view);
    Button createAppointment = (Button)findViewById(R.id.crtApp);
    createAppointment.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            startActivity(new Intent("android.intent.action.CREATEAPP"));           
        }
    });
}

LOG CAT

04-25 15:17:13.719: W/dalvikvm(460): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-25 15:17:13.779: E/AndroidRuntime(460): FATAL EXCEPTION: main
04-25 15:17:13.779: E/AndroidRuntime(460): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CREATEAPP }
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.app.Activity.startActivityForResult(Activity.java:2817)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.app.Activity.startActivity(Activity.java:2923)
04-25 15:17:13.779: E/AndroidRuntime(460):  at com.examples.SimpleCalendarViewActivity$1.onClick(SimpleCalendarViewActivity.java:60)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.view.View.performClick(View.java:2408)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.view.View$PerformClick.run(View.java:8816)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.os.Handler.handleCallback(Handler.java:587)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.os.Looper.loop(Looper.java:123)
04-25 15:17:13.779: E/AndroidRuntime(460):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 15:17:13.779: E/AndroidRuntime(460):  at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:17:13.779: E/AndroidRuntime(460):  at java.lang.reflect.Method.invoke(Method.java:521)
04-25 15:17:13.779: E/AndroidRuntime(460):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 15:17:13.779: E/AndroidRuntime(460):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 15:17:13.779: E/AndroidRuntime(460):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 3555

Answers (5)

Ski6154
Ski6154

Reputation: 1

<Button /> needs to have the android:onClick="your method name here"attribute

and Java code should look something like this

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

Upvotes: 0

Asaf Nevo
Asaf Nevo

Reputation: 11688

Have you declared the activity in the menifest file?

Besides try:

Intent i = new Intent();
i.setClassName(pkgname, activity name);
startActivity(i);

Upvotes: 0

Iulia Barbu
Iulia Barbu

Reputation: 1530

if I understand well, you want to start an activity on button pressed.Then you should have something like this :

createAppointment.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
     Intent intent = new Intent(your_context, CreateAppointment.class);
     startActivity(intent);
     }
 });

hope it helps! (for better understanding post your error log :) )

Upvotes: 3

Carnal
Carnal

Reputation: 22064

startActivity(new Intent(getBaseContext(), CreateAppointment.class));

And also like kailoon said, "Check to make sure the activity is in the AndroidManifest.xml file."

Upvotes: 0

kailoon
kailoon

Reputation: 2069

Check to make sure the activity is in the AndroidManifest.xml file.

Upvotes: 0

Related Questions