1

我希望在按下“createAppointment”按钮时启动一个新活动,但到目前为止,当我单击该按钮时,应用程序崩溃了。

按钮的创建:

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

创建应用程序.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>

创建约会.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);
    }

}

按钮开始新活动的代码

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"));           
        }
    });
}

日志猫

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)
4

5 回答 5

3

如果我理解得很好,你想在按下按钮时开始一个活动。那么你应该有这样的东西:

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

希望能帮助到你!(为了更好地理解发布您的错误日志:))

于 2012-04-25T15:15:37.653 回答
0

<Button />需要有android:onClick="your method name here"属性

Java 代码应该是这样的

public void methodName(View view)
{
  Intent intent = new Intent(this, ClassName.class);
  startActivity(intent)
}
于 2013-07-08T20:07:21.793 回答
0
startActivity(new Intent(getBaseContext(), CreateAppointment.class));

并且也像 kailoon 所说,“检查以确保活动在 AndroidManifest.xml 文件中。”

于 2012-04-25T15:12:33.780 回答
0

您是否在清单文件中声明了该活动?

除了尝试:

Intent i = new Intent();
i.setClassName(pkgname, activity name);
startActivity(i);
于 2012-04-25T15:12:48.957 回答
0

检查以确保活动在 AndroidManifest.xml 文件中。

于 2012-04-25T15:10:51.007 回答