通过点击一个按钮,启动另一项活动 [英] Start another activity by clicking a button

查看:100
本文介绍了通过点击一个按钮,启动另一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的继承人问题。我设置,当点击应该打开一个新的活动按钮,但点击时,我收到一个错误:遗憾的是APP_NAME已停止工作 我的logcat说:致命异常主要

So heres my problem. I set up a button that when clicked should open a new activity but when it is clicked I receive an error: Unfortunately "app_name" has stopped working my logcat says :Fatal Exception Main

所以,我的继承人的xml:

So heres my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="58dp"
        android:text="Monday"
        android:textSize="20sp"/>

       <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Tuesday" />

       <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Wednesday" />

       <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Thursday" />

       <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Friday" />

       <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Saturday" />

       <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        android:text="Sunday" />

       <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView1"
           android:layout_alignBottom="@+id/textView1"
           android:layout_alignParentRight="true"
           android:layout_marginRight="46dp"
           android:text="Edit"
           android:onClick="mondayintent" />

       <Button
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView2"
           android:layout_alignBottom="@+id/textView2"
           android:layout_alignLeft="@+id/button1"
           android:text="Edit" />

       <Button
           android:id="@+id/button3"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView3"
           android:layout_alignBottom="@+id/textView3"
           android:layout_alignLeft="@+id/button2"
           android:text="Edit" />

       <Button
           android:id="@+id/button4"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView4"
           android:layout_alignBottom="@+id/textView4"
           android:layout_alignLeft="@+id/button3"
           android:text="Edit" />

       <Button
           android:id="@+id/button5"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView5"
           android:layout_alignBottom="@+id/textView5"
           android:layout_alignLeft="@+id/button4"
           android:text="Edit" />

       <Button
           android:id="@+id/button6"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView6"
           android:layout_alignBottom="@+id/textView6"
           android:layout_alignLeft="@+id/button5"
           android:text="Edit" />

       <Button
           android:id="@+id/button7"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignBaseline="@+id/textView7"
           android:layout_alignBottom="@+id/textView7"
           android:layout_alignLeft="@+id/button6"
           android:text="Edit" />
</RelativeLayout>

和第二个XML到我试着去达到另一个活动:

and the second Xml to which im trying to reach in another activity:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textSize="40sp"
        android:text="Monday" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="36dp"
        android:layout_toLeftOf="@+id/textView1"
        android:ems="3"
        android:inputType="time" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:ems="3"
        android:inputType="time" >
        <requestFocus />
    </EditText>

      <Spinner 
        android:id="@+id/profileSelector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/editText2"
        android:layout_toRightOf="@+id/textView1"
        android:ems="3" 
        android:prompt="@string/profile"
        android:entries="@array/profileSelector"/>
</RelativeLayout>

最后,code

second activity code:

第二项活动code:

package com.example.hush; import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Monday extends Activity{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.monday); } }

Below I have added my android-manifest.xml

下面我增加了我的Andr​​oid-manifest.xml的

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hush" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.hush.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.hush.Monday" android:label="@string/app_name" > <intent-filter> <action android:name="com.example.hush.Monday" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> </application> </manifest>


推荐答案

您需要调用按钮,这样另一个活动的OnClickListener内()

You need to call another activity like this inside the OnClickListener() of button

Button mondayEdit= (Button)findViewById(R.id.button1);
    mondayEdit.setOnClickListener(new OnClickListener() 
    {   public void onClick(View v) 
        {   
            Intent intent = new Intent(main.this, secondActivity.class);
                startActivity(intent);      
                finish();
        }
    });

这篇关于通过点击一个按钮,启动另一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆