Android Studio emulator white screen upon opening of app - java

Whenever I install and run my app in the Android Studio emulator all I get is a white screen and the tool bar above it even though in the preview it works completely fine. Heres the xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#drawable/rectanglestemlogo"
android:orientation="horizontal">
<Button
android:id="#+id/Home_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:onClick="goToStemAcademy"
android:text="#string/Home_Button"
android:textAllCaps="false"
android:textColor="#000000" />
<Button
android:id="#+id/StemAcademy_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:text="#string/Stem_Academy"
android:textAllCaps="false"
android:textColor="#000000"
android:visibility="visible" />
<Button
android:id="#+id/Competitions_Button"
android:layout_width="0dp"
android:layout_weight="0.33"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:text="#string/Competitions"
android:textAllCaps="false"
android:textColor="#000000" />
</LinearLayout>
then the java:
package com.example.dmssteamapp6;
import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public void goToStemAcademy(View v) {
startActivity(new Intent(MainActivity.this, StemAcademy.class));
}
}
then the new xml 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">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="138dp"
android:onClick="goToHome"
android:text="Button" />
</LinearLayout>
and the java that goes with it:
package com.example.dmssteamapp6;
import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class StemAcademy extends AppCompatActivity {
public void goToHome(View v) {
startActivity(new Intent(StemAcademy.this, MainActivity.class));
}
}
also here is the androidmanifest.xml code if applicable:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dmssteamapp6">
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StemAcademy"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
</application>
</manifest>

Just Create onCreate method in every java class
public class StemAcademy extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stemacademy);
}
public void goToHome(View v) {
startActivity(new Intent(StemAcademy.this, MainActivity.class));
}
}
Activity main class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void goToStemAcademy(View v) {
startActivity(new Intent(MainActivity.this, StemAcademy.class));
}
}

Related

how can i play sound in android

I wrote simplw android program to play sound and it's not working . Have no idea why .
maybe it's related to the fact that i have question mark near mymp3 file ( under raw directory ).
I tried few options without any suseess .
Attached below is my code , thanks in advance .
xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:background="#CFEC89"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="200dp"
android:layout_height="80dp"
android:id="#+id/sound"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_gravity="center"
android:text="Sound"
android:textSize="30sp"
android:background="#EAE3E3"
/>
</LinearLayout>
main acitivy java
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
// define my variables
Button sound;
final MediaPlayer mp = MediaPlayer.create(this,R.raw.cat);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v)
{
mp.start();
}
}
and my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.playsound">
<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/Theme.PlaySound">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
First of all, delete your TextView it could lay over your Button. That's why you probably can't click it.
Paste that in your class:
package com.example.playsound;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.cat);
final Button sound = (Button) this.findViewById(R.id.sound);
sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.start();
}
});
}
}
For the future, you need to define your variables in the OnCreate and not above.

Shared Elements Transition won't work correctly when layout background is set

I tried to follow some tutorials on how to set up a shared elements transition. Though when i try to set up a background the shared element kind of glitches the wrong way and then moves like it should.
Heres a link to a video of my problem: https://drive.google.com/file/d/14lTFGIpqnMwM2MEbLxwBe_gkI0GuyuMc/view?usp=sharing
When no background is set: https://drive.google.com/open?id=15Hpkj7y8nLLTXa5RF5FV00jlTTWMofjF
When i don't set a background in my xml layout, the animation works properly. Does anyone know, why this happens?
Activity 1 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#color/colorPrimary"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/nexa"
android:gravity="center"
android:padding="10dp"
android:text="Test"
android:textColor="#color/colorAccent"
android:textSize="30sp"
android:transitionName="transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity 2 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="Test"
android:fontFamily="#font/nexa"
android:textColor="#color/colorAccent"
android:textSize="30sp"
android:transitionName="transition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity 1 Java
package com.stonks.sharedanimtest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setEnterTransition(null);
textView = (TextView) findViewById(R.id.text1);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, textView, "transition");
startActivity(intent, options.toBundle());
}
});
}
}
Activity 2 Java
package com.stonks.sharedanimtest;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
private TextView textView;
#Override
public void onBackPressed() {
textView = (TextView) findViewById(R.id.text2);
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity2.this, textView, "transition");
startActivity(intent, options.toBundle());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setEnterTransition(null);
}
}
Colors XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#161618</color>
<color name="colorPrimaryLight">#202024</color>
<color name="colorAccent">#ffffff</color>
</resources>
Manifest XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stonks.sharedanimtest">
<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=".MainActivity2"
android:screenOrientation="portrait" />
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can wrap your shared view (either text1 or text2) in FrameLayout - but still TextView should be the shared element. It should solve the issue.

My app crashes when I clicked the Imagebutton instead of taking me to another Activity

I'm making an android app on Android Studio. And I come out with a problem . When I click my ImageButton the app crashes instead of going to a new activity (Slide1)...
I dont know where I got it wrong .
Here is my MainActivity.java
package android.example.ptreinas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
private ImageButton imageButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
openSlide1();
}
});
}
public void openSlide1(){
Intent intent = new Intent(this, Slide1.class);
startActivity(intent);
}
}
and my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:gravity="center"
tools:context=".MainActivity"
android:background="#color/red">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="400px"
android:layout_gravity="center"
android:src="#drawable/gym" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Programa de treinos "
android:layout_gravity="center"
android:textColor="#ffffff"
android:textStyle="bold"
android:textSize="80px"
android:layout_marginTop="50px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Escolha o programa de treino mais adequado aos seus objetivos."
android:layout_gravity="center"
android:textAlignment="center"
android:layout_marginTop="50px"
android:textSize="50px"
android:textColor="#ffffff"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#drawable/ic_right"
android:background="#drawable/roundcorner"
android:padding="50px"
android:layout_gravity="center"
android:layout_marginTop="150px"
/>
</LinearLayout>
</RelativeLayout>
Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<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>
</application>
</manifest>
I dont know if you need the second activity (slide1.xml , Slide1.java) let me know
I need your help, thanks in advance .
replace this :
Intent intent = new Intent(this, Slide1.class);
by this:
Intent intent = new Intent(MainActivity.this, Slide1.class);
add your second activity in manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.example.ptreinas">
<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>
//add this line
<activity android:name=".Slide1"/>
</application>

the android app crashes or not installs after building using android studio

When i run my app in my device [which runs on 7.1] The app runs well but when i run it on my opo which runs on 4.4 it crashed and when i try to run it on a android 5.0 device it gave app cannot install error
I've build>generate signed apk and have used that
I'm basically new to all this so if anyone can help me out what the issue is i would be glad
my activity_main code is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.bingobean.pitchblack.MainActivity"
android:weightSum="1">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:src="#drawable/android"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="Pitch black"
android:textColor="#ffffff"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="118dp"
android:layout_height="109dp"
android:layout_marginTop="144dp"
android:text="Hit it!"
android:textSize="24sp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
My mainactivity is
package com.bingobean.pitchblack;
import android.app.WallpaperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button btn;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
img = (ImageView) findViewById(R.id.imageView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WallpaperManager wallmgr =
WallpaperManager.getInstance(getApplicationContext());
try {
wallmgr.setResource(+ R.drawable.android);
} catch (IOException e){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Wallpaper is set",
Toast.LENGTH_SHORT).show();
}
});
}
}
My Androidmanifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bingobean.pitchblack">
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<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>
</application>
</manifest>
check your app level build.gradle file in , in that file You have these two attributes :
minSdkVersion 16
targetSdkVersion 23 this
// these are api levels
Change them to according to your requirement .
Ditto to what Hamza said, you must have set your min and target api to not be compatible with kitkat. as for the app not installed, that is either 1: you have an app with same package name installed, 2: the LAUNCH and DEFAULT intents aren't set (they are it seems), or 3: it somehow got corrupted. Please post your logcat for when it crashes. Root your phone and download LogCat.

Why can't I send an array of 1000000 ints from one Activity to another?

I'm trying to second an array of 1000000 ints from one Activity to another. It works fine with smaller numbers, but when I try 1000000, startActivity does nothing, and causes this to show in logcat:
E/JavaBinder(2239): !!! FAILED BINDER TRANSACTION !!!
Why?
Here's some code demonstrating the issue:
MainActivity.java
package com.example.a;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startSecond(View v) {
startActivity(new Intent(this, SecondActivity.class).putExtra(
"a", new int[1000000]));
}
}
SecondActivity.java
package com.example.a;
import android.os.Bundle;
import android.app.Activity;
public class SecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
activity_main.xml
<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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:layout_marginTop="31dp"
android:onClick="startSecond"
android:text="click clack" />
</RelativeLayout>
activity_second.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>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
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.a.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.a.SecondActivity"></activity>
</application>
</manifest>
Why?
Because you are limited to 1MB per Binder transaction, and Binder underlies the Intent system. The size of your Intent, including all extras, needs to be under 1MB.

Categories

Resources