Open second Activity error in Android Studio [duplicate] - android

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I was trying to make small application of two activities but it gives me a code error...............................................................................................................................................................................................................................................
the error
Main activity Java code:
package com.example.amr.startnewactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
private Button op_btn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickButtonListener();
}
public void onClickButtonListener()
{
op_btn= (Button)findViewById(R.id.button);
op_btn.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(".secondActivity");
startActivity(intent);
}
}
);
}
}
Second Activity Java code:
package com.example.amr.startnewactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class secondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Android manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amr.startnewactivity">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".secondActivity"></activity>
<intent-filter>
<action android:name=".secondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</application>
</manifest>
Main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="152dp"
android:layout_height="wrap_content"
android:layout_marginStart="116dp"
android:layout_marginLeft="116dp"
android:layout_marginTop="248dp"
android:text="open"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Second activity .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".secondActivity">
</android.support.constraint.ConstraintLayout>

did you try this?
Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);

add this code to your button click
Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);
OR
startActivity(new Intent(MainActivity.this,secondActivity.class))
And try to follow the naming conventions in Java. Look at here

What you're looking for is Intent(Context context, Class<?> class):
startActivity(new Intent(MainActivity.this, SecondActivity.class);
Or:
Intent secondActivityIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(secondActivityIntent);
For more information, check out the documentation ("Start another activity").
A second note: Consider removing the <intent-filter> from your AndroidManifest.xml file for secondActivity. This declares a filter for other apps to access programmatically.
In this case, you don't actually need it if you're using an Activity. This is typically used for Intents.

Related

android studio 3 : Error:(26, 105) error: illegal character: '\ufeff'

I was trying to do this Tutorial
But there's an error then try to do with the top comment seen at below
Why do I still got an error Error:(26, 105) error: illegal character: '\ufeff'
Here is my code
package com.pete.mseuf.intent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static Button Button_Submit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener(){
Button_Submit = (Button)findViewById(R.id.button);
Button_Submit.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this ,
Main2Activity.class);
startActivity(intent);
}
}
);
}
}
Added my xml file from AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pete.mseuf.intent">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="com.pete.mseuf.intent.Main2Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my Layout xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pete.mseuf.intent.MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="168dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I am new here at stackoverflow and in android studio. Thankyou for your patience
Possibilities :you have miss typed the character '\ufeff' in your XML
Open build.gradle (Module: app) form Gradle Scripts section in your android studio project
Add this line
compileOptions.encoding = 'windows-1251'
inside android {//add line hare} like this
android {
compileOptions.encoding = 'windows-1251'
}
clean the project and run..
I hope it will solve your problem...
feel free to ask..

Cant start new activity. It keeps crashing

When pressing the button, new activity should open but app instantly crashes. I have searched for answer and making a few adjustments to my code and only thing i dont know how to do is Manifest, can u help me with that?
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Starting.Programm"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity android:name="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="InformationActivity"
android:label="#string/app_name"
>
</activity>
</application>
MainActivity:
package Starting.Programm;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends InformationActivity
{
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startJourney(View view) {
Intent intent = new Intent(this, InformationActivity.class);
startActivity(intent);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
>
<Button android:id="#+id/button_StartApp"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="#string/button_StartApp"
android:layout_gravity="center"
android:layout_marginTop="70dp"
android:layout_marginLeft="115dp"
android:onClick="startJourney"
/>
</LinearLayout>
And next activity which should be opening
package Starting.Programm;
import android.app.Activity;
import android.os.Bundle;
class InformationActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
}
}
<activity> android:name=".InformationActivity" </activity>
Remove android:label="#string/app_name" too. I don't think it is needed.
Add dot to end of pacakager name
package="Starting.Programm."

How to make two activities?

I'm new in Android studio and I'm trying to make two activities in my application but it don't works.
I don't know what I'm doing wrong, I think it's about the first options I put in my intent.
I'm new and I would like a simple solution or if you can explain me what I have to do it will be fine :)
This is my first activity (MainActivity):
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mPasserelle = null;
public final static String AGE = "com.myapplis.multiactivite.AGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPasserelle = (Button) findViewById(R.id.passerelle);
mPasserelle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent secondeActivite = new Intent(MainActivity.this,IntentExample.class);
secondeActivite.putExtra(AGE,24);
startActivity(secondeActivite);
}
});
}
}
The second activity(IntentExample) :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentExample extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.seconde_activite);
Intent i = getIntent();
int age = i.getIntExtra(MainActivity.AGE,0);
TextView resultat= (TextView)findViewById(R.id.resultat);
resultat.setText("Le résultat est : "+age);
}
}
This is my first layout (activity_main), just a simple button to push to get the second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dunomade.multiactivite.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/passerelle"/>
</LinearLayout>
And now this is my second layout(seconde_activite), just a simple text to show me I'm on the second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultat"
android:text="" />
</LinearLayout>
And to finish, this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplis.multiactivite">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntentExample"
android:label="seconde acivite">
</activity>
</application>
</manifest>
When I'm running the app, MainActivity is good, but when I try to click on the button the AVD show me "Have you declared this activity in your AndroidManifest ?". I declared it on my manifest but I think the problem is in my MainActivity.
Please help me, I think for you it's really simple but I can't solve it, I tryed many way but I already got the same error.
Thank's for read and for answer =)
I advise that you use the name of package before the name of your activity, like this (some devices need this, like Moto G) :
<activity android:name="your.package.IntentExample "></activity>
And instead use:
public class IntentExample extends Activity
use:
public class IntentExample extends AppCompatActivity

Android layout can not display in device

My Android Studio version is 1.5.1
I have created an XML and it can show well in the AndroidStudio,
but when i run it in genymotion or my phone,it can not show the layout.
I am annoyed at this.I do not know what is wrong.
hope someone could help me.
code image
code:
SplashActivity
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class SplashActivity extends AppCompatActivity {
private Button mEnterButton;
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.enter_button:
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
break;
}
}
};
#Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_splash);
mEnterButton = (Button) findViewById(R.id.enter_button);
mEnterButton.setOnClickListener(mOnClickListener);
}
}
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SplashActivity.class);
startActivity(intent);
}
});
}
}
avtivity_splash_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#color/splashBackground">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/splash_tv_text"
android:textSize="28sp"
android:id="#+id/text_splash"
android:textColor="#color/white"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/splash_click_to_inter"
android:id="#+id/enter_button"/>
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geekband.geekbandprojects">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
</activity>
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Why you need a PersistableBundle ?
make your OnCreateMethod like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//anything here
}
i'm considering that you haven't already set the SplashActivity as the startup activity.So add the following code(or change it) on Manifest:
<activity
android:name=".Splash"
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=".MainActivity"/>
And let us know your MainActivity code and of course your Manifest with the logcat.
I will updated my answer after that.
And of course, i think you should use onClick on Oncreate method.

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).
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>
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.
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>
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
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>
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
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
}
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
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>
setContentView(R.layout.avtivity_next);
I think this line of code should be moved to the next activity...
Below code is working fine with Android 4.3:
Intent i = new Intent(this,MainActivity2.class);
startActivity(i);
First you have to declare the activity in Manifest. It is important. You can add this inside application like this.
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..
Register your java class on Android manifest file
After that write this code on button click
startActivity(new intent(MainActivity.this,NextActivity.class));
You can do
Intent i = new Intent(classname.this , targetclass.class);
startActivity(i);

Categories

Resources