Transition from one action to another. Android activity.

I want to move from one action to another (using a virtual device). When I click on the button to move, a dialog box appears in my emulator with the image unfortunately SMS1 has stopped working (SMS1 is my application name).

Can someone help me fix 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 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> 
+8
android android-intent android-emulator relativelayout
source share
14 answers

You have not defined NextActivity in the AndroidManifest.xml file.

Add these lines to the android manifest after the </activity> . 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> 
+9
source share

You must first use this code in the 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 convey the intention in this way.

second

add the correct entry to the manifest.xml file.

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

Now let's see what happens.

+17
source share

Just add NextActivity to the Manifest.XML file

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

button1 in action2

code recorded in step 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 can help

+3
source share

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

2) if you have not yet defined the following activity in the application manifest file, follow these steps:

 <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 need to complete the second step every time you create a new action, otherwise your application will crash

+1
source share

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

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

add activity to androidManifest file as well

  <activity android:name=".SignUpActivity"></activity> 
+1
source share

setContentView (R.layout.avtivity_next);

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

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

this is a direct way to move the second action, and there is no need to call

0
source share

Below code works fine with Android 4.3:

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

This is mainly due to unregistered activity in the manifest file as "NextActivity" First register NextActivity in the manifest, for example

 <activity android:name=".NextActivity"> 

then use the code where you want

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

where you should call NextActivity ..

0
source share

First you need to declare the action in the manifest. It is important. You can add this internal application like this.

0
source share

Register your Java class in the Android manifest file

After that write this code when you click

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

You can do

 Intent i = new Intent(classname.this , targetclass.class); startActivity(i); 
-2
source share

All Articles