Android layout can not display in device - android

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.

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.

How we start an activity in MainActivity which extends AppCompatActivity [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
Closed 4 years ago.
I used startActivity() method but it is not working.I think this method is not working because my MainActivity extends app compat actvity.
So please help me to start activity in an app compat activity.
I try various times but when I start my apk and click on button then app crashes.
Here my code
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E96050"/>
<TableRow><Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainButton"
android:text="Open Second Activity"
android:onClick="yas"/></TableRow> </TableLayout>
MainActivity.java
package com.mycompany.myapp;
import android.app.Activity;
import android.os.*;
import android.support.v7.app.*;
import
android.support.v7.widget.Toolbar;
import android.view.*;
import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.*;
public class MainActivity extends
AppCompatActivity {
#Override
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// TODO: Implement this method
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
private void CreateMenu(Menu menu) {
MenuItem mnu1 = menu.add(0,0,0,"item1");
{
mnu1.setIcon(R.drawable.ic_launcher);
mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public void yas(View vv) {
Intent iiii = new
Intent(MainActivity.this,SecondAct.class);
startActivity(iiii);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<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>
<activity
android:name=".SecondActivity"
android:label="secondActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="SecondActivity" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
use this
Intent intent=new Intent(this,Target_Activity.class);
startActivity(intent);
And open your manifest file add this line
<activity android:name=".Target_Activity"
android:parentActivityName=".MainActivity">
</activity>
I hope you try this code..
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
Please add second activity into android manifest file.
<activity android:name=".SecondActivity"/> // define second activity name.
add this code in android manifest file..
<activity
android:name=".SecondActivity"
android:label="secondActivity" >
</activity>

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

splash screen shown as blank screen

I made a splash layout, that last for a time and then other activity should be loaded,alone it works perfectly, but when it's connected to other activity, it shows blank screen.
mainifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andy.propaganda"
android:installLocation="auto" >
<application
android:allowBackup="true"
android:icon="#drawable/my_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.NoActionBar"
>
<activity
android:name=".shit"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="com.coderefer.androidsplashscreenexample.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<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>
<!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
splash activity :
package andy.propaganda;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Andrew on 2/4/2016.
*/
public class Splash extends Activity {
//welcome animation
ImageView loading_img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
loading_img = (ImageView)findViewById(R.id.loading_view);
final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim);
loading_img.setAnimation(animatable);
//
for(int i = 0;i< 100000000;i++);
Intent intent = new Intent(this, shit.class);
intent.putExtra("jhjh", 8);
startActivity(intent);
}
}
main activity :
package andy.propaganda;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by Andrew on 2/5/2016.
*/
public class shit extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
splash.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/MyBlue"
>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/my_launcher"
android:paddingTop="60dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#drawable/loading"
android:layout_marginBottom="43dp"
android:id="#+id/loading_view"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="loading your files"
android:id="#+id/welcome_message"
android:focusable="false"
android:textColor="#010101"
android:textSize="#dimen/abc_action_bar_progress_bar_size"
android:layout_above="#+id/loading_view"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView"
android:layout_marginBottom="50dp" />
</RelativeLayout>
You should use timer on splash screen instead of for(int i = 0;i< 100000000;i++); which will wait for some time suppose 5 sec and than load another Activity.
package andy.propaganda;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
/**
* Created by Andrew on 2/4/2016.
*/
public class Splash extends Activity {
//welcome animation
ImageView loading_img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
loading_img = (ImageView)findViewById(R.id.loading_view);
final Animation animatable = AnimationUtils.loadAnimation(this, R.anim.welcome_screen_anim);
loading_img.setAnimation(animatable);
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// your task
Intent intent = new Intent(this, shit.class);
intent.putExtra("jhjh", 8);
startActivity(intent);
}
}, 5000);
}
}
RxJava solution:
Observable.empty().subscribeOn(AndroidSchadelurs.mainThread()).delay(500, TimeUtils.MILISECONDS).subscribe(this::startActivity(this, new Intent));
First of All dont use for Loop for waiting time instead of That use Handler with something like this :
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(Splash.this, shit.class));
finish();
}
}, 500);
Then remove this line from your manifest.xml :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Rest of your code seems ok :)
Also take look at this link which well-explained about Splash Screen.

Error Execution failed for task

i'm a beginner in Android
and i'm trying different programs but this error still show up
':app:processDebugManifest' Manifest merger failed with multiple errors
how i can fix it
this is my code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.nhn.test">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:icon,android:theme"
>
<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"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<activity android:name=".Main2Activity" >
</activity>
</application>
<android:replace>"android:icon"</android:replace>
</manifest>
this is the first layout
package com.example.nhn.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void BtnNex(View view){
Intent int1= new Intent(this,Main2Activity.class);
startActivity(int1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
this is the second had one button
package com.example.nhn.test;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
public void BtnPrev(View view){
Intent int2= new Intent(this,MainActivity.class);
startActivity(int2);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

Categories

Resources