开始一个新的活动按钮按下按钮
我想要一个新的活动开始,当我按下“createAppointment”按钮,但到目前为止,当我点击按钮,应用程序崩溃。开始一个新的活动按钮按下按钮
的按钮创建:
<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);
}
}
代码,开始一个新的活动
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)
:
createAppointment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(your_context, CreateAppointment.class);
startActivity(intent);
}
});
希望它能帮助! (为了更好的理解发表您的错误日志:))
你的意思是“your_context” – 2012-04-25 15:22:06
你应该在一个活动没有?然后用
它工作吗? :d – 2012-04-25 15:29:35
检查以确保该活动是在AndroidManifest.xml文件。
我已经在therooid manifest.xml中添加了活动,并且我将编辑以发布logcat – 2012-04-25 15:13:16
startActivity(new Intent(getBaseContext(), CreateAppointment.class));
而且还像kailoon说的,“检查以确保活动在AndroidManifest.xml文件中。”
谢谢mate !!!!!跳起来!!!!!当它工作!!!!! loooool btw getBaseContext()是什么意思? – 2012-04-25 15:40:50
getBaseContext()返回上下文,你也可以使用Activity.this。但要小心使用新的意图(这,Lala.class),因为在你的情况下,“this”将引用OnClick而不是你的活动 – Carnal 2012-04-25 16:44:34
你是否在清单文件中声明了活动?
除了尝试:
Intent i = new Intent();
i.setClassName(pkgname, activity name);
startActivity(i);
<Button />
需要有android:onClick="your method name here"
属性
和Java代码应该是这个样子
public void methodName(View view)
{
Intent intent = new Intent(this, ClassName.class);
startActivity(intent)
}
发布你所得到的logcat的错误。 – dymmeh 2012-04-25 15:10:11