Moving from one activity to another Activity in Android

我的未来我决定 提交于 2019-11-26 10:36:31

问题


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

Can anybody help me in correcting my code?

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

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

<?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

<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

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

回答1:


You haven't defined NextActivity in the AndroidManifest.xml file.

Add these lines in android manifest after</activity> tag. 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>



回答2:


First You have to use this code in MainActivity.java class

@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.

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

Now see what happens.




回答3:


Simply add your NextActivity in the Manifest.XML file

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



回答4:


button1 in activity2

code written in activity 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




回答5:


1) place setContentView(R.layout.avtivity_next); to the next-activity's onCreate() method just like this (main) activity's onCreate()

2) if you have not defined the next-activity in your-apps manifest file then do this also, like:

<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




回答6:


When you have to go from one page to another page in android changes made in 2 files

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

add activity in androidManifest file also like

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



回答7:


setContentView(R.layout.avtivity_next);

I think this line of code should be moved to the next activity...




回答8:


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




回答9:


Below code is working fine with Android 4.3:

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



回答10:


First you have to declare the activity in Manifest. It is important. You can add this inside application like this.




回答11:


It is mainly due to unregistered activity in manifest file as "NextActivity" Firstly register NextActivity in Manifest like

<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..




回答12:


Register your java class on Android manifest file

After that write this code on button click

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



回答13:


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

}



回答14:


You can do

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


来源:https://stackoverflow.com/questions/17526533/moving-from-one-activity-to-another-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!