简体   繁体   English

在Android中从一个活动转移到另一个活动

[英]Moving from one activity to another Activity in Android

I want to move from one activity to another (using virtual device). 我想从一个活动转移到另一个活动(使用虚拟设备)。 When I click on button to move, My emulator ones a dialog box showing unfortunately SMS1 has stopped working (SMS1 is my app name). 当我点击按钮移动时,我的模拟器会显示一个对话框,显示unfortunately SMS1 has stopped working (SMS1是我的应用程序名称)。

Can anybody help me in correcting my code? 任何人都可以帮我纠正我的代码吗?

MainActivity.java: MainActivity.java:

package com.example.sms1;

 import android.os.Bundle;
 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;
 import android.widget.TextView;

 public class MainActivity extends Activity implements OnClickListener
 {

Button b1;
TextView tv1;
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button1);
    tv1 = (TextView) findViewById(R.id.textView1);

    b1.setOnClickListener(this);

 }

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

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(),NextActivity.class);
    startActivity(i);
    setContentView(R.layout.avtivity_next);
}



}

Here is the NextActivity 这是NextActivity

package com.example.sms1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class NextActivity extends Activity {

TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.avtivity_next);
    tv1 = (TextView) findViewById(R.id.textView1);
}


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

}

Manifest.XML 的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms1"
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.sms1.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>
</application>

</manifest>

NextActivityLayout NextActivityLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NextActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="next activity" />



</RelativeLayout>

MainActivity Layout MainActivity布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="80dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Button" />

</RelativeLayout>

First You have to use this code in MainActivity.java class 首先,您必须在MainActivity.java类中使用此代码

@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(),NextActivity.class);
    startActivity(i);

}

You can pass intent this way. 你可以通过这种方式传递意图。

Second 第二

add proper entry into manifest.xml file. manifest.xml文件中添加适当的条目。

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

Now see what happens. 现在看看会发生什么。

You haven't defined NextActivity in the AndroidManifest.xml file. 您尚未在AndroidManifest.xml文件中定义NextActivity

Add these lines in android manifest after </activity> tag. </activity>标记后面的android清单中添加这些行。 It should work. 它应该工作。

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

final code will be 最终的代码将是

<application
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="Main Activity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NextActivity" >
    </activity>
</application>

Simply add your NextActivity in the Manifest.XML file 只需在Manifest.XML文件中添加NextActivity

<activity
            android:name="com.example.sms1.NextActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

button1 in activity2 activity2中的button1

code written in activity 2 活动2中写的代码

button1.setOnClickListener(new View.OnClickListener() {         
        public void onClick(View v)
        {
            // starting background task to update product               
            Intent fp=new Intent(getApplicationContext(),activity1.class);              
            startActivity(fp);              
        }
    });

This might help 这可能有所帮助

1) place setContentView(R.layout.avtivity_next); 1)放置setContentView(R.layout.avtivity_next); to the next-activity's onCreate() method just like this (main) activity's onCreate() 到下一个活动的onCreate()方法就像这个(主要)活动的onCreate()

2) if you have not defined the next-activity in your-apps manifest file then do this also, like: 2)如果你还没有在你的应用程序清单文件中定义下一个活动,那么也要这样做,例如:

<application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="Main Activity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NextActivity"
            android:label="Next Activity" >
        </activity>
    </application>

You must have to perform the 2nd step every time you create a new activity, otherwise your app will crash 每次创建新活动时都必须执行第2步,否则您的应用程序将崩溃

When you have to go from one page to another page in android changes made in 2 files 当你必须从一个页面转到另一个页面的android更改在2个文件中

Intent intentSignUP = new Intent(this,SignUpActivity.class);
   startActivity(intentSignUP);

add activity in androidManifest file also like 在androidManifest文件中添加活动也就像

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

setContentView(R.layout.avtivity_next); 的setContentView(R.layout.avtivity_next);

I think this line of code should be moved to the next activity... 我认为这行代码应该转移到下一个活动......

public void onClick(View v)
{
 startActivity(new Intent(getApplicationContext(), Next.class));

}

it is direct way to move second activity and there is no need for call intent 它是移动第二个活动的直接方式,不需要呼叫意图

Below code is working fine with Android 4.3: 下面的代码适用于Android 4.3:

Intent i = new Intent(this,MainActivity2.class);
startActivity(i);

It is mainly due to unregistered activity in manifest file as "NextActivity" Firstly register NextActivity in Manifest like 这主要是由于清单文件中未注册的活动为“NextActivity”首先在Manifest中注册NextActivity

<activity android:name=".NextActivity">

then use the code in the where you want 然后在你想要的地方使用代码

Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);

where you have to call the NextActivity.. 你必须调用NextActivity ..

First you have to declare the activity in Manifest. 首先,您必须在Manifest中声明活动。 It is important. 这很重要。 You can add this inside application like this. 您可以在此应用程序中添加此内容。

Register your java class on Android manifest file 在Android清单文件上注册您的java类

After that write this code on button click 之后在按钮上单击编写此代码

startActivity(new intent(MainActivity.this,NextActivity.class));
@Override
public void onClick(View v)
{
    // TODO Auto-generated method stub
    Intent intent = new Intent(Activity1.this,Activity2.class);
    startActivity(intent);

}

You can do 你可以做

Intent i = new Intent(classname.this , targetclass.class);
startActivity(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM