Cant start new activity. It keeps crashing - android

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

Related

Open second Activity error in Android Studio [duplicate]

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.

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.

java.lang.IllegalStateException: Could not execute method of the activity - starting new Activity

I am trying to start a New Activity using Intent but I keep getting java.lang.IllegalStateException: Could not execute method of the activity
What am I doing wrong?
Main Activity
package com.example.wrw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void clicked(View v)
{
Intent intent = new Intent(this, newact.class);
startActivity(intent);
}
}
newact.java
package com.example.wrw;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class newact extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlayout);
}
}
act_main.xml
<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"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Act"
android:onClick="clicked"
/>
</LinearLayout>
newlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrw"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
</application>
</manifest>
In android app, all Activity must be configured in the Manifest.xml file firstly, so you should add newact Activity to your manifest
<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="com.example.wrw.newact"></activity>
Add this to your manifest file
<activity
android:name=".newact"
android:label="#string/title_activity_login">
Replace this to your MainActivity, this may resolve your problem.
Intent intent = new Intent(MainActivity.this, newact.class);
startActivity(intent);
After this, do not forget to add this in your android menifest file,
<activity
android:name=".newact"
android:label="#string/app_name">
</activity>
try this
Intent intent = new Intent(MainActivity.this, newact.class)
and also register newact in manifest file like this
<activity
android:name=".newact"
android:label="#string/app_name">
</activity>

Android application crases when switching activity

I have a problem with an Android aplication. When I try to swich from main activity (PretvornikValut) to another one (Pretvornik) via buttun I get the "Unfortunately aplication stopped working" error. Can anyone help?
PretvornikValut.java
package pretvornik.valut;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class PretvornikValut extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View gumbPretvori = findViewById(R.id.gumb_pretvornik);
gumbPretvori.setOnClickListener(this);
View gumbTecaji = findViewById(R.id.gumb_tecaji);
gumbTecaji.setOnClickListener(this);
}
public void sendMessage(View view) {
Intent intent = new Intent(this, Pretvornik.class);
startActivity(intent);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.gumb_pretvornik:
Intent i = new Intent(this, Pretvornik.class);
//i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
break;
case R.id.gumb_tecaji:
Intent tecaj = new Intent(this, Tecaji.class);
tecaj.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(tecaj);
break;
}
}
}
Pretvornik java
package pretvornik.valut;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
/**
*
* #author FAKS
*/
public class Pretvornik extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.pretvornik);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99CCCC"
>
<Button
android:id="#+id/gumb_pretvornik"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Pretvornik"
/>
<Button
android:id="#+id/gumb_tecaji"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="#+id/OK"
android:layout_alignParentBottom="true"
android:text="Izpis tecajev"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Izberi zeljeno opcijo"
/>
</LinearLayout>
pretvornik.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#99CCCC"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="glavni program"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pretvornik.valut"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="PretvornikValut"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name="Pretvornik"
android:label="#string/Pretvornik_valut" >
<meta-data
android:name="PretvornikValut"
/>
</activity>
<activity
android:name="Tecaji"
android:label="#string/Izpis_tecajev" >
<meta-data
android:name="PretvornikValut"
/>
</activity>
</activity>
</application>
</manifest>
Change your manifest xml,
add action for your activities
action android:name="android.intent.action.ACTIVITY"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pretvornik.valut"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="PretvornikValut"
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="Pretvornik"
android:label="#string/hello_world" >
<action android:name="android.intent.action.ACTIVITY" />
</activity>
<activity
android:name="Tecaji"
android:label="#string/hello_world" >
<action android:name="android.intent.action.ACTIVITY" />
</activity>
</application>
</manifest>

Is that possible to steal the message sent by Intents?

Update:
I have spent more than 2 hours to google, but I can't find the answer. I've add a main.java the handle the activity send from the Login Activity. Now the file tree looks like:
But still got error
E/AndroidRuntime(1282): android.content.ActivityNotFoundException: No
Activity found to handle Intent {
act=com.goodboy.loginIntent.action.main
cat=[android.intent.category.DEFAULT] (has extras) }
I know this question is simple, I am new to android, any help would be appreciated:)
Android allows for Intents that have specific recipients(the Activity or Service) as well as Intents that are broadcast throughout the system to any components that may be listening.
I want to make a PoC(Proof of Concept) that if we do not set setClassName, others can listen your private message.
This PoC is simple, suppose there is Login Activity for App Goodboy, when a user put his username and password in the login activity, and click the login button, the evil activity from App Badboy steal the this message.
However, failed:(
When I click the login button, failed:
And the evil intent got nothing:
The java source code of Login Activity
package com.goodboy.loginIntent;
import com.goodboy.loginIntent.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class GoodloginActivity extends Activity {
private EditText et_user;
private EditText et_pwd;
private Button btn_login;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
et_user = (EditText) findViewById(R.id.et_user);
et_pwd = (EditText) findViewById(R.id.et_pwd);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent m_intent = new Intent();
m_intent.putExtra("username", et_user.getText().toString());
m_intent.putExtra("password", et_pwd.getText().toString());
m_intent.setAction("com.goodboy.loginIntent.action.main");
m_intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(m_intent);
}
});
}
}
The source code of main.java
package com.goodboy.loginIntent;
import android.app.Activity;
import android.os.Bundle;
import com.goodboy.loginIntent.R;
public class main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
The login layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/et_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
The java source code of evil activity:
package com.badboy.stealIntent;
import com.badboy.stealIntent.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class BadIntentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(getBaseContext(),
"username: "+this.getIntent().getStringExtra("username")+
"\npassword: "+this.getIntent().getStringExtra("password"),
Toast.LENGTH_SHORT).show();
}
}
Thanks #David Wasser, the manifest of login app(update):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goodboy.loginIntent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".GoodloginActivity"
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=".main"
android:label="#string/main" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The manifest of the badIntent:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.badboy.stealIntent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BadIntentActivity"
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>
You should take a look at Intent Intercept, an application aimed for developer which, as the name says, intercept any "public" Intent, allowing you to browse the intent setup and data. Intent Intercept is Open source, you can browse the code on GitHub
As for your problem, check that the BadBoy application is registered for the action you're using. Also, take a look at the stacktrace in logcat on goodlogin to see where the activity crashes.
You are probably getting an ActivityNotFoundException when you call startActivity() in GoodloginActivity because there is no Activity known to the system that responds to:
ACTION = "com.goodboy.loginIntent.action.main" and
CATEGORY = CATEGORY_DEFAULT
Have a look at your logcat output.

Categories

Resources