jackEarlyDays
jackEarlyDays

Reputation: 217

Android Studio: New activity opens as blank screen

Hello!
I'm just starting in Android Studio. I searched for a matching question but no joy, please yell out if you've seen this one already!

My main activity has a single button which opens the second activity, the button works and it opens. But the second activity shows as a blank screen instead of the text that should be there.

Apologies for any irrelevant copy/paste!

Manifest:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.NoActionBar" >
        <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=".fiveThreeOne"
            android:label="@string/title531">
        </activity>
    </application>

Main activity:

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:textColor="#ffdedede"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fiveThreeOne"
        android:textAllCaps="false"
        android:id="@+id/open531btn"
        android:layout_below="@+id/mainTitle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="38dp" />

</RelativeLayout>

Button code in main class

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button open531button = (Button) findViewById(R.id.open531btn);
        open531button.setOnClickListener(new OnClickListener(){

            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, fiveThreeOne.class));
            }
        });

    }

Second activity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/title531"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:editable="false"
        android:textSize="35sp"
        android:id="@+id/title531"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />


</RelativeLayout>

Thanks!

Upvotes: 16

Views: 26227

Answers (5)

pear95
pear95

Reputation: 66

In my case, I changed device in Android Virtual Divice Manager and this fix problem

Upvotes: 0

Ankul Abhishek Roy
Ankul Abhishek Roy

Reputation: 1

You can just go to dependencies in build.gradle file and add this line there.. & try.

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

Upvotes: 0

shekhar g h
shekhar g h

Reputation: 1251

It sometimes happens when instead of overloading

protected void onCreate(Bundle savedInstanceState) {

you end up overloading

public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) 

instead. Make sure the onCreate with only savedInstanceState as a argument is overloaded.

Upvotes: 97

bonney
bonney

Reputation: 465

Have you set the layout for your activity in the line setContentView() like this. Here activity_second is the activity layout of my SecondActivity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

if not then your activity will show up as blank. Hope this solves your problem.

Upvotes: 17

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3687

Check weather you have set the correct layout for the setContentView() method of your second activity.

Upvotes: 0

Related Questions